Spaces:
Build error
Build error
Started File Sharing
Browse files- README.md +45 -13
- app.py +93 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,13 +1,45 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: File Sharing System
|
| 3 |
-
emoji: 💻
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: green
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.31.5
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
license: apache-2.0
|
| 11 |
-
---
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: File Sharing System
|
| 3 |
+
emoji: 💻
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.31.5
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: apache-2.0
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# File Storing and Sharing System
|
| 14 |
+
|
| 15 |
+
This project is a file storing and sharing system built using Gradio for the front-end interface and MongoDB for the back-end database. The system allows users to upload files, which are then stored and managed within a specified directory. URLs for accessing these files are generated and stored in a MongoDB collection, making it easy to share and access the uploaded files.
|
| 16 |
+
|
| 17 |
+
## Key Components
|
| 18 |
+
|
| 19 |
+
### Gradio
|
| 20 |
+
Gradio is used to create the front-end interface, allowing users to interact with the file upload system through a simple and intuitive GUI.
|
| 21 |
+
|
| 22 |
+
### MongoDB
|
| 23 |
+
MongoDB is used to store metadata about the uploaded files, specifically the URLs where these files can be accessed. This project connects to a MongoDB instance using credentials stored in environment variables for security.
|
| 24 |
+
|
| 25 |
+
## Project Structure
|
| 26 |
+
|
| 27 |
+
### Environment Variables
|
| 28 |
+
- `SECRET_KEY`: Authetication Key to access data.
|
| 29 |
+
- `MONGO_USERNAME`: MongoDB username.
|
| 30 |
+
- `MONGO_PASSWORD`: MongoDB password.
|
| 31 |
+
- `REST_URI`: MongoDB URI for connecting to the database.
|
| 32 |
+
|
| 33 |
+
## How to Run
|
| 34 |
+
|
| 35 |
+
1. **Set Environment Variables**: Make sure to set the `SECRET_KEY`, `MONGO_USERNAME`, `MONGO_PASSWORD`, and `REST_URI` environment variables with your MongoDB credentials and URI.
|
| 36 |
+
2. **Install Dependencies**: Ensure you have `gradio`, `pymongo`, and other necessary packages installed.
|
| 37 |
+
3. **Run the Application**: Execute the script to start the Gradio interface and handle file uploads.
|
| 38 |
+
|
| 39 |
+
```bash
|
| 40 |
+
python your_script.py
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
## Conclusion
|
| 44 |
+
|
| 45 |
+
This project provides a simple yet effective solution for file storing and sharing. By leveraging Gradio for the front-end and MongoDB for the back-end, it ensures a seamless experience for users to upload and manage their files with easy access through generated URLs.
|
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from pymongo.mongo_client import MongoClient
|
| 5 |
+
from pymongo.server_api import ServerApi
|
| 6 |
+
import urllib.parse
|
| 7 |
+
from bson.objectid import ObjectId
|
| 8 |
+
|
| 9 |
+
username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME'))
|
| 10 |
+
password = urllib.parse.quote_plus(os.getenv('MONGO_PASSWORD'))
|
| 11 |
+
restUri = os.getenv('REST_URI')
|
| 12 |
+
uri = f'mongodb+srv://{username}:{password}{restUri}'
|
| 13 |
+
client = MongoClient(uri, server_api=ServerApi('1'))
|
| 14 |
+
db = client['file_storage']
|
| 15 |
+
references_collection = db['references']
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
client.admin.command('ping')
|
| 19 |
+
print("Pinged your deployment. You successfully connected to MongoDB!")
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(e)
|
| 22 |
+
|
| 23 |
+
theme = gr.themes.Default(
|
| 24 |
+
primary_hue="blue",
|
| 25 |
+
secondary_hue="violet",
|
| 26 |
+
neutral_hue="slate",
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
def upload(file):
|
| 30 |
+
root_directory = '/tmp/gradio'
|
| 31 |
+
folder_contents_dict = get_folder_contents_dict(root_directory)
|
| 32 |
+
print(folder_contents_dict)
|
| 33 |
+
|
| 34 |
+
base_url = "https://abhicodes-file-uploader-component.hf.space/file=/tmp/gradio"
|
| 35 |
+
|
| 36 |
+
urls = [f"{base_url}/{folder}/{file}" for folder, files in folder_contents_dict.items() for file in files]
|
| 37 |
+
|
| 38 |
+
print(json.dumps(urls, indent=4))
|
| 39 |
+
|
| 40 |
+
references_collection.update_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"$set": {"urls": urls}})
|
| 41 |
+
|
| 42 |
+
gr.Info("Uploaded Successfully")
|
| 43 |
+
|
| 44 |
+
def get_folder_contents_dict(root_dir):
|
| 45 |
+
folder_contents = {}
|
| 46 |
+
for item in os.listdir(root_dir):
|
| 47 |
+
item_path = os.path.join(root_dir, item)
|
| 48 |
+
if os.path.isdir(item_path):
|
| 49 |
+
contents = os.listdir(item_path)
|
| 50 |
+
folder_contents[item] = contents
|
| 51 |
+
|
| 52 |
+
return folder_contents
|
| 53 |
+
|
| 54 |
+
def generate_markdown(json_data):
|
| 55 |
+
urls = json_data.get("urls", [])
|
| 56 |
+
markdown_lines = []
|
| 57 |
+
for url in urls:
|
| 58 |
+
# Encode URL components
|
| 59 |
+
encoded_url = urllib.parse.quote(url, safe=':/')
|
| 60 |
+
filename = url.split('/')[-1]
|
| 61 |
+
encoded_filename = urllib.parse.quote(filename)
|
| 62 |
+
markdown_lines.append(f'- <a style="text-decoration:none; font-size:18px" href={encoded_url} target="_blank">{encoded_filename}</a>')
|
| 63 |
+
return "\n".join(markdown_lines)
|
| 64 |
+
|
| 65 |
+
def get_uploads(secret):
|
| 66 |
+
if secret == os.environ.get('SECRET_KEY'):
|
| 67 |
+
result = references_collection.find_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"_id": 0})
|
| 68 |
+
if result:
|
| 69 |
+
markdown_output = generate_markdown(result)
|
| 70 |
+
return markdown_output
|
| 71 |
+
else:
|
| 72 |
+
return {"error": "No result found"}
|
| 73 |
+
else:
|
| 74 |
+
return {"error": "Unauthorized"}
|
| 75 |
+
|
| 76 |
+
with gr.Blocks(theme=theme) as demo:
|
| 77 |
+
gr.Markdown('''<h1 style="text-align:center;">File Storing and Sharing System</h1>''')
|
| 78 |
+
gr.Markdown('''This project is a file storing and sharing system built using Gradio for the front-end interface and MongoDB for the back-end database. The system allows users to upload files, which are then stored and managed within a specified directory. URLs for accessing these files are generated and stored in a MongoDB collection, making it easy to share and access the uploaded files.
|
| 79 |
+
> To know more read the docs at: [Documentation](https://huggingface.co/spaces/abhicodes/file-sharing-system/blob/main/README.md)
|
| 80 |
+
''')
|
| 81 |
+
with gr.Row():
|
| 82 |
+
with gr.Column():
|
| 83 |
+
gr.Markdown('''<h1 style="text-align:center;">Uploader</h1>''')
|
| 84 |
+
input = gr.File(label="Input", file_count="multiple")
|
| 85 |
+
input.upload(fn=upload, inputs=input)
|
| 86 |
+
with gr.Column():
|
| 87 |
+
gr.Markdown('''<h1 style="text-align:center;">Downloader</h1>''')
|
| 88 |
+
secret_key = gr.Text(placeholder="Enter your Secret Key")
|
| 89 |
+
uploads = gr.Markdown(label="Uploads")
|
| 90 |
+
get_upload_button = gr.Button("Get Uploads", variant='primary')
|
| 91 |
+
get_upload_button.click(fn=get_uploads, inputs=secret_key, outputs=uploads)
|
| 92 |
+
|
| 93 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pymongo[srv]
|