Spaces:
Build error
Build error
AdarshRajDS commited on
Commit Β·
b04d4d4
1
Parent(s): b4517e4
Fix HF Spaces runtime 3
Browse files
app.py
CHANGED
|
@@ -12,21 +12,18 @@ BACKEND_URL = "https://adarshds-thesisbackend.hf.space"
|
|
| 12 |
# -----------------------------
|
| 13 |
def ask_question(message, history):
|
| 14 |
try:
|
| 15 |
-
|
| 16 |
f"{BACKEND_URL}/rag/ask",
|
| 17 |
json={"question": message},
|
| 18 |
timeout=120,
|
| 19 |
)
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
images_md = ""
|
| 24 |
for img in data.get("images", []):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
answer = data["answer"] + images_md
|
| 28 |
|
| 29 |
-
history.append((message, answer))
|
| 30 |
return "", history
|
| 31 |
|
| 32 |
except Exception as e:
|
|
@@ -35,19 +32,18 @@ def ask_question(message, history):
|
|
| 35 |
|
| 36 |
|
| 37 |
# -----------------------------
|
| 38 |
-
# π UPLOAD
|
| 39 |
# -----------------------------
|
| 40 |
def upload_pdf(file):
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
f"{BACKEND_URL}/upload/pdf",
|
| 44 |
files={"file": file},
|
| 45 |
timeout=300,
|
| 46 |
)
|
| 47 |
-
return
|
| 48 |
-
|
| 49 |
except Exception as e:
|
| 50 |
-
return
|
| 51 |
|
| 52 |
|
| 53 |
# -----------------------------
|
|
@@ -55,19 +51,17 @@ def upload_pdf(file):
|
|
| 55 |
# -----------------------------
|
| 56 |
def visualize(image, question):
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
f"{BACKEND_URL}/visualize",
|
| 60 |
files={"image": open(image, "rb")},
|
| 61 |
data={"question": question},
|
| 62 |
timeout=120,
|
| 63 |
)
|
| 64 |
-
|
| 65 |
-
data = response.json()
|
| 66 |
|
| 67 |
return data["answer"], f"{BACKEND_URL}/{data['output_image']}"
|
| 68 |
-
|
| 69 |
except Exception as e:
|
| 70 |
-
return
|
| 71 |
|
| 72 |
|
| 73 |
# -----------------------------
|
|
@@ -75,60 +69,48 @@ def visualize(image, question):
|
|
| 75 |
# -----------------------------
|
| 76 |
def grade(image):
|
| 77 |
try:
|
| 78 |
-
|
| 79 |
f"{BACKEND_URL}/grade-annotation/",
|
| 80 |
files={"file": open(image, "rb")},
|
| 81 |
timeout=120,
|
| 82 |
)
|
| 83 |
-
|
| 84 |
-
return response.json()["result"]
|
| 85 |
-
|
| 86 |
except Exception as e:
|
| 87 |
-
return
|
| 88 |
|
| 89 |
|
| 90 |
# ==================================================
|
| 91 |
-
#
|
| 92 |
# ==================================================
|
| 93 |
|
| 94 |
-
with gr.Blocks(
|
| 95 |
|
| 96 |
gr.Markdown("# π§ Multimodal RAG Anatomy Tutor")
|
| 97 |
|
| 98 |
-
# π¬ CHAT
|
| 99 |
with gr.Tab("Chat"):
|
| 100 |
chatbot = gr.Chatbot(height=500)
|
| 101 |
-
msg = gr.Textbox(
|
| 102 |
msg.submit(ask_question, [msg, chatbot], [msg, chatbot])
|
| 103 |
|
| 104 |
-
# π UPLOAD
|
| 105 |
with gr.Tab("Upload PDF"):
|
| 106 |
file = gr.File(file_types=[".pdf"])
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
|
| 111 |
-
# π§ VISUALIZE
|
| 112 |
with gr.Tab("Visualize Anatomy"):
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
vis_btn.click(
|
| 120 |
-
visualize,
|
| 121 |
-
[vis_image, vis_question],
|
| 122 |
-
[vis_answer, vis_output],
|
| 123 |
-
)
|
| 124 |
|
| 125 |
-
# π GRADING
|
| 126 |
with gr.Tab("Annotation Grading"):
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
|
| 132 |
|
| 133 |
-
# β
THIS IS THE ONLY SAFE LAUNCH FOR HF SPACES
|
| 134 |
demo.queue().launch()
|
|
|
|
| 12 |
# -----------------------------
|
| 13 |
def ask_question(message, history):
|
| 14 |
try:
|
| 15 |
+
r = requests.post(
|
| 16 |
f"{BACKEND_URL}/rag/ask",
|
| 17 |
json={"question": message},
|
| 18 |
timeout=120,
|
| 19 |
)
|
| 20 |
+
data = r.json()
|
| 21 |
|
| 22 |
+
images = ""
|
|
|
|
|
|
|
| 23 |
for img in data.get("images", []):
|
| 24 |
+
images += f"\n\n"
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
history.append((message, data["answer"] + images))
|
| 27 |
return "", history
|
| 28 |
|
| 29 |
except Exception as e:
|
|
|
|
| 32 |
|
| 33 |
|
| 34 |
# -----------------------------
|
| 35 |
+
# π UPLOAD
|
| 36 |
# -----------------------------
|
| 37 |
def upload_pdf(file):
|
| 38 |
try:
|
| 39 |
+
r = requests.post(
|
| 40 |
f"{BACKEND_URL}/upload/pdf",
|
| 41 |
files={"file": file},
|
| 42 |
timeout=300,
|
| 43 |
)
|
| 44 |
+
return r.json()["message"]
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
+
return str(e)
|
| 47 |
|
| 48 |
|
| 49 |
# -----------------------------
|
|
|
|
| 51 |
# -----------------------------
|
| 52 |
def visualize(image, question):
|
| 53 |
try:
|
| 54 |
+
r = requests.post(
|
| 55 |
f"{BACKEND_URL}/visualize",
|
| 56 |
files={"image": open(image, "rb")},
|
| 57 |
data={"question": question},
|
| 58 |
timeout=120,
|
| 59 |
)
|
| 60 |
+
data = r.json()
|
|
|
|
| 61 |
|
| 62 |
return data["answer"], f"{BACKEND_URL}/{data['output_image']}"
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
+
return str(e), None
|
| 65 |
|
| 66 |
|
| 67 |
# -----------------------------
|
|
|
|
| 69 |
# -----------------------------
|
| 70 |
def grade(image):
|
| 71 |
try:
|
| 72 |
+
r = requests.post(
|
| 73 |
f"{BACKEND_URL}/grade-annotation/",
|
| 74 |
files={"file": open(image, "rb")},
|
| 75 |
timeout=120,
|
| 76 |
)
|
| 77 |
+
return r.json()["result"]
|
|
|
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
+
return str(e)
|
| 80 |
|
| 81 |
|
| 82 |
# ==================================================
|
| 83 |
+
# UI
|
| 84 |
# ==================================================
|
| 85 |
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
|
| 88 |
gr.Markdown("# π§ Multimodal RAG Anatomy Tutor")
|
| 89 |
|
|
|
|
| 90 |
with gr.Tab("Chat"):
|
| 91 |
chatbot = gr.Chatbot(height=500)
|
| 92 |
+
msg = gr.Textbox()
|
| 93 |
msg.submit(ask_question, [msg, chatbot], [msg, chatbot])
|
| 94 |
|
|
|
|
| 95 |
with gr.Tab("Upload PDF"):
|
| 96 |
file = gr.File(file_types=[".pdf"])
|
| 97 |
+
btn = gr.Button("Upload & Ingest")
|
| 98 |
+
out = gr.Textbox()
|
| 99 |
+
btn.click(upload_pdf, file, out)
|
| 100 |
|
|
|
|
| 101 |
with gr.Tab("Visualize Anatomy"):
|
| 102 |
+
img = gr.Image(type="filepath")
|
| 103 |
+
q = gr.Textbox()
|
| 104 |
+
btn = gr.Button("Visualize")
|
| 105 |
+
ans = gr.Textbox()
|
| 106 |
+
out_img = gr.Image()
|
| 107 |
+
btn.click(visualize, [img, q], [ans, out_img])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
|
|
|
| 109 |
with gr.Tab("Annotation Grading"):
|
| 110 |
+
img = gr.Image(type="filepath")
|
| 111 |
+
btn = gr.Button("Grade")
|
| 112 |
+
res = gr.Textbox(lines=15)
|
| 113 |
+
btn.click(grade, img, res)
|
| 114 |
|
| 115 |
|
|
|
|
| 116 |
demo.queue().launch()
|