Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,93 +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-
|
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)
|
|
|
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-sharing-system.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)
|