Spaces:
Running
Running
import gradio as gr | |
import os | |
import json | |
from pymongo.mongo_client import MongoClient | |
from pymongo.server_api import ServerApi | |
import urllib.parse | |
from bson.objectid import ObjectId | |
username = urllib.parse.quote_plus(os.getenv('MONGO_USERNAME')) | |
password = urllib.parse.quote_plus(os.getenv('MONGO_PASSWORD')) | |
restUri = os.getenv('REST_URI') | |
uri = f'mongodb+srv://{username}:{password}{restUri}' | |
client = MongoClient(uri, server_api=ServerApi('1')) | |
db = client['file_storage'] | |
references_collection = db['references'] | |
try: | |
client.admin.command('ping') | |
print("Pinged your deployment. You successfully connected to MongoDB!") | |
except Exception as e: | |
print(e) | |
theme = gr.themes.Default( | |
primary_hue="blue", | |
secondary_hue="violet", | |
neutral_hue="slate", | |
) | |
def upload(file): | |
root_directory = '/tmp/gradio' | |
folder_contents_dict = get_folder_contents_dict(root_directory) | |
print(folder_contents_dict) | |
base_url = "https://abhicodes-file-uploader-component.hf.space/file=/tmp/gradio" | |
urls = [f"{base_url}/{folder}/{file}" for folder, files in folder_contents_dict.items() for file in files] | |
print(json.dumps(urls, indent=4)) | |
references_collection.update_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"$set": {"urls": urls}}) | |
gr.Info("Uploaded Successfully") | |
def get_folder_contents_dict(root_dir): | |
folder_contents = {} | |
for item in os.listdir(root_dir): | |
item_path = os.path.join(root_dir, item) | |
if os.path.isdir(item_path): | |
contents = os.listdir(item_path) | |
folder_contents[item] = contents | |
return folder_contents | |
def generate_markdown(json_data): | |
urls = json_data.get("urls", []) | |
markdown_lines = [] | |
for url in urls: | |
# Encode URL components | |
encoded_url = urllib.parse.quote(url, safe=':/') | |
filename = url.split('/')[-1] | |
encoded_filename = urllib.parse.quote(filename) | |
markdown_lines.append(f'- <a style="text-decoration:none; font-size:18px" href={encoded_url} target="_blank">{encoded_filename}</a>') | |
return "\n".join(markdown_lines) | |
def get_uploads(secret): | |
if secret == os.environ.get('SECRET_KEY'): | |
result = references_collection.find_one({"_id": ObjectId('66531a797cbaa19ba4e441c5')}, {"_id": 0}) | |
if result: | |
markdown_output = generate_markdown(result) | |
return markdown_output | |
else: | |
return {"error": "No result found"} | |
else: | |
return {"error": "Unauthorized"} | |
with gr.Blocks(theme=theme) as demo: | |
gr.Markdown('''<h1 style="text-align:center;">File Storing and Sharing System</h1>''') | |
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. | |
> To know more read the docs at: [Documentation](https://huggingface.co/spaces/abhicodes/file-sharing-system/blob/main/README.md) | |
''') | |
with gr.Row(): | |
with gr.Column(): | |
gr.Markdown('''<h1 style="text-align:center;">Uploader</h1>''') | |
input = gr.File(label="Input", file_count="multiple") | |
input.upload(fn=upload, inputs=input) | |
with gr.Column(): | |
gr.Markdown('''<h1 style="text-align:center;">Downloader</h1>''') | |
secret_key = gr.Text(placeholder="Enter your Secret Key") | |
uploads = gr.Markdown(label="Uploads") | |
get_upload_button = gr.Button("Get Uploads", variant='primary') | |
get_upload_button.click(fn=get_uploads, inputs=secret_key, outputs=uploads) | |
demo.launch(debug=True) |