Update app.py
Browse files
app.py
CHANGED
|
@@ -4,42 +4,55 @@ import shutil
|
|
| 4 |
import gradio as gr
|
| 5 |
from supabase import create_client, Client
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
url = os.environ["SUPABASE_URL"]
|
| 8 |
key = os.environ["SUPABASE_SERVICE_KEY"]
|
| 9 |
|
| 10 |
supabase: Client = create_client(url, key)
|
| 11 |
BUCKET = "examfiles"
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def upload_to_supabase(local_path, remote_path):
|
| 14 |
with open(local_path, "rb") as f:
|
| 15 |
supabase.storage.from_(BUCKET).upload(
|
| 16 |
-
remote_path,
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
return public_url
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def process_upload(qp, ms, ans):
|
| 22 |
timestamp = str(int(time.time()))
|
| 23 |
temp_dir = "/tmp/upload"
|
| 24 |
os.makedirs(temp_dir, exist_ok=True)
|
| 25 |
|
| 26 |
-
urls = []
|
| 27 |
-
|
| 28 |
def handle_file(gr_file):
|
| 29 |
if gr_file is None:
|
| 30 |
return None
|
| 31 |
|
| 32 |
-
#
|
| 33 |
src = gr_file if isinstance(gr_file, str) else gr_file["name"]
|
| 34 |
original_name = os.path.basename(src)
|
| 35 |
|
| 36 |
local_path = os.path.join(temp_dir, original_name)
|
| 37 |
shutil.copy(src, local_path)
|
| 38 |
|
| 39 |
-
# Store under: examfiles/<timestamp>/<original_filename>
|
| 40 |
remote_path = f"{timestamp}/{original_name}"
|
| 41 |
-
|
| 42 |
-
return url
|
| 43 |
|
| 44 |
qp_url = handle_file(qp)
|
| 45 |
ms_url = handle_file(ms)
|
|
@@ -49,8 +62,13 @@ def process_upload(qp, ms, ans):
|
|
| 49 |
if qp_url: output += f"{qp_url}\n"
|
| 50 |
if ms_url: output += f"{ms_url}\n"
|
| 51 |
if ans_url: output += f"{ans_url}\n"
|
|
|
|
| 52 |
return output
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown("### Upload PDFs (original filenames preserved)")
|
| 56 |
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from supabase import create_client, Client
|
| 6 |
|
| 7 |
+
# -------------------------------------------------------
|
| 8 |
+
# Supabase config
|
| 9 |
+
# -------------------------------------------------------
|
| 10 |
url = os.environ["SUPABASE_URL"]
|
| 11 |
key = os.environ["SUPABASE_SERVICE_KEY"]
|
| 12 |
|
| 13 |
supabase: Client = create_client(url, key)
|
| 14 |
BUCKET = "examfiles"
|
| 15 |
|
| 16 |
+
|
| 17 |
+
# -------------------------------------------------------
|
| 18 |
+
# Upload file to Supabase Storage
|
| 19 |
+
# -------------------------------------------------------
|
| 20 |
def upload_to_supabase(local_path, remote_path):
|
| 21 |
with open(local_path, "rb") as f:
|
| 22 |
supabase.storage.from_(BUCKET).upload(
|
| 23 |
+
remote_path,
|
| 24 |
+
f,
|
| 25 |
+
file_options={"upsert": "true"} # IMPORTANT FIX
|
| 26 |
)
|
| 27 |
+
|
| 28 |
+
# Construct public URL
|
| 29 |
+
public_url = (
|
| 30 |
+
f"{url}/storage/v1/object/public/{BUCKET}/{remote_path}"
|
| 31 |
+
)
|
| 32 |
return public_url
|
| 33 |
|
| 34 |
+
|
| 35 |
+
# -------------------------------------------------------
|
| 36 |
+
# Processing upload for each file
|
| 37 |
+
# -------------------------------------------------------
|
| 38 |
def process_upload(qp, ms, ans):
|
| 39 |
timestamp = str(int(time.time()))
|
| 40 |
temp_dir = "/tmp/upload"
|
| 41 |
os.makedirs(temp_dir, exist_ok=True)
|
| 42 |
|
|
|
|
|
|
|
| 43 |
def handle_file(gr_file):
|
| 44 |
if gr_file is None:
|
| 45 |
return None
|
| 46 |
|
| 47 |
+
# Gradio returns dict or filepath depending on environment
|
| 48 |
src = gr_file if isinstance(gr_file, str) else gr_file["name"]
|
| 49 |
original_name = os.path.basename(src)
|
| 50 |
|
| 51 |
local_path = os.path.join(temp_dir, original_name)
|
| 52 |
shutil.copy(src, local_path)
|
| 53 |
|
|
|
|
| 54 |
remote_path = f"{timestamp}/{original_name}"
|
| 55 |
+
return upload_to_supabase(local_path, remote_path)
|
|
|
|
| 56 |
|
| 57 |
qp_url = handle_file(qp)
|
| 58 |
ms_url = handle_file(ms)
|
|
|
|
| 62 |
if qp_url: output += f"{qp_url}\n"
|
| 63 |
if ms_url: output += f"{ms_url}\n"
|
| 64 |
if ans_url: output += f"{ans_url}\n"
|
| 65 |
+
|
| 66 |
return output
|
| 67 |
|
| 68 |
+
|
| 69 |
+
# -------------------------------------------------------
|
| 70 |
+
# Gradio UI
|
| 71 |
+
# -------------------------------------------------------
|
| 72 |
with gr.Blocks() as demo:
|
| 73 |
gr.Markdown("### Upload PDFs (original filenames preserved)")
|
| 74 |
|