Spaces:
Build error
Build error
AdarshRajDS commited on
Commit Β·
f5b28b8
1
Parent(s): 1558d13
Fix HF Spaces runtime 1
Browse files
app.py
CHANGED
|
@@ -1,16 +1,15 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
# β
HF Spaces safe settings
|
| 6 |
-
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 7 |
-
|
| 8 |
BACKEND_URL = "https://adarshds-thesisbackend.hf.space"
|
| 9 |
|
| 10 |
|
| 11 |
-
#
|
| 12 |
# π¬ RAG CHAT
|
| 13 |
-
#
|
| 14 |
def ask_question(message, history):
|
| 15 |
try:
|
| 16 |
response = requests.post(
|
|
@@ -31,13 +30,13 @@ def ask_question(message, history):
|
|
| 31 |
return "", history
|
| 32 |
|
| 33 |
except Exception as e:
|
| 34 |
-
history.append((message, f"β
|
| 35 |
return "", history
|
| 36 |
|
| 37 |
|
| 38 |
-
#
|
| 39 |
# π UPLOAD PDF
|
| 40 |
-
#
|
| 41 |
def upload_pdf(file):
|
| 42 |
try:
|
| 43 |
response = requests.post(
|
|
@@ -48,12 +47,12 @@ def upload_pdf(file):
|
|
| 48 |
return response.json()["message"]
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
return f"β
|
| 52 |
|
| 53 |
|
| 54 |
-
#
|
| 55 |
# π§ VISUALIZE
|
| 56 |
-
#
|
| 57 |
def visualize(image, question):
|
| 58 |
try:
|
| 59 |
response = requests.post(
|
|
@@ -65,18 +64,15 @@ def visualize(image, question):
|
|
| 65 |
|
| 66 |
data = response.json()
|
| 67 |
|
| 68 |
-
return
|
| 69 |
-
data["answer"],
|
| 70 |
-
f"{BACKEND_URL}/{data['output_image']}",
|
| 71 |
-
)
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
-
return f"β
|
| 75 |
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
# π GRADE
|
| 79 |
-
#
|
| 80 |
def grade(image):
|
| 81 |
try:
|
| 82 |
response = requests.post(
|
|
@@ -88,45 +84,35 @@ def grade(image):
|
|
| 88 |
return response.json()["result"]
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
-
return f"β
|
| 92 |
|
| 93 |
|
| 94 |
-
# ==================================================
|
| 95 |
# π¨ UI
|
| 96 |
-
# ==================================================
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
analytics_enabled=False,
|
| 100 |
-
show_api=False, # π₯ CRITICAL FIX
|
| 101 |
-
) as demo:
|
| 102 |
|
| 103 |
gr.Markdown("# π§ Multimodal RAG Anatomy Tutor")
|
| 104 |
|
| 105 |
-
#
|
| 106 |
-
with gr.Tab("
|
| 107 |
chatbot = gr.Chatbot(height=500)
|
| 108 |
msg = gr.Textbox(placeholder="Ask anatomy question...")
|
|
|
|
| 109 |
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
[msg, chatbot],
|
| 113 |
-
[msg, chatbot],
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
-
# ================= UPLOAD =================
|
| 117 |
-
with gr.Tab("π Upload PDF"):
|
| 118 |
file = gr.File(file_types=[".pdf"])
|
| 119 |
upload_btn = gr.Button("Upload & Ingest")
|
| 120 |
upload_output = gr.Textbox()
|
| 121 |
-
|
| 122 |
upload_btn.click(upload_pdf, file, upload_output)
|
| 123 |
|
| 124 |
-
#
|
| 125 |
-
with gr.Tab("
|
| 126 |
vis_image = gr.Image(type="filepath")
|
| 127 |
vis_question = gr.Textbox(label="Ask about the structure")
|
| 128 |
vis_btn = gr.Button("Visualize")
|
| 129 |
-
|
| 130 |
vis_answer = gr.Textbox()
|
| 131 |
vis_output = gr.Image()
|
| 132 |
|
|
@@ -136,15 +122,13 @@ with gr.Blocks(
|
|
| 136 |
[vis_answer, vis_output],
|
| 137 |
)
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
with gr.Tab("
|
| 141 |
grade_image = gr.Image(type="filepath")
|
| 142 |
grade_btn = gr.Button("Grade")
|
| 143 |
-
|
| 144 |
grade_result = gr.Textbox(lines=15)
|
| 145 |
-
|
| 146 |
grade_btn.click(grade, grade_image, grade_result)
|
| 147 |
|
| 148 |
|
| 149 |
-
# β
|
| 150 |
-
demo.queue(
|
|
|
|
| 1 |
import os
|
| 2 |
+
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 3 |
+
|
| 4 |
import gradio as gr
|
| 5 |
import requests
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
BACKEND_URL = "https://adarshds-thesisbackend.hf.space"
|
| 8 |
|
| 9 |
|
| 10 |
+
# -----------------------------
|
| 11 |
# π¬ RAG CHAT
|
| 12 |
+
# -----------------------------
|
| 13 |
def ask_question(message, history):
|
| 14 |
try:
|
| 15 |
response = requests.post(
|
|
|
|
| 30 |
return "", history
|
| 31 |
|
| 32 |
except Exception as e:
|
| 33 |
+
history.append((message, f"β {str(e)}"))
|
| 34 |
return "", history
|
| 35 |
|
| 36 |
|
| 37 |
+
# -----------------------------
|
| 38 |
# π UPLOAD PDF
|
| 39 |
+
# -----------------------------
|
| 40 |
def upload_pdf(file):
|
| 41 |
try:
|
| 42 |
response = requests.post(
|
|
|
|
| 47 |
return response.json()["message"]
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
+
return f"β {str(e)}"
|
| 51 |
|
| 52 |
|
| 53 |
+
# -----------------------------
|
| 54 |
# π§ VISUALIZE
|
| 55 |
+
# -----------------------------
|
| 56 |
def visualize(image, question):
|
| 57 |
try:
|
| 58 |
response = requests.post(
|
|
|
|
| 64 |
|
| 65 |
data = response.json()
|
| 66 |
|
| 67 |
+
return data["answer"], f"{BACKEND_URL}/{data['output_image']}"
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
+
return f"β {str(e)}", None
|
| 71 |
|
| 72 |
|
| 73 |
+
# -----------------------------
|
| 74 |
+
# π GRADE
|
| 75 |
+
# -----------------------------
|
| 76 |
def grade(image):
|
| 77 |
try:
|
| 78 |
response = requests.post(
|
|
|
|
| 84 |
return response.json()["result"]
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
+
return f"β {str(e)}"
|
| 88 |
|
| 89 |
|
| 90 |
+
# ==================================================
|
| 91 |
# π¨ UI
|
| 92 |
+
# ==================================================
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(analytics_enabled=False) as demo:
|
|
|
|
|
|
|
|
|
|
| 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(placeholder="Ask anatomy question...")
|
| 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 |
upload_btn = gr.Button("Upload & Ingest")
|
| 108 |
upload_output = gr.Textbox()
|
|
|
|
| 109 |
upload_btn.click(upload_pdf, file, upload_output)
|
| 110 |
|
| 111 |
+
# π§ VISUALIZE
|
| 112 |
+
with gr.Tab("Visualize Anatomy"):
|
| 113 |
vis_image = gr.Image(type="filepath")
|
| 114 |
vis_question = gr.Textbox(label="Ask about the structure")
|
| 115 |
vis_btn = gr.Button("Visualize")
|
|
|
|
| 116 |
vis_answer = gr.Textbox()
|
| 117 |
vis_output = gr.Image()
|
| 118 |
|
|
|
|
| 122 |
[vis_answer, vis_output],
|
| 123 |
)
|
| 124 |
|
| 125 |
+
# π GRADING
|
| 126 |
+
with gr.Tab("Annotation Grading"):
|
| 127 |
grade_image = gr.Image(type="filepath")
|
| 128 |
grade_btn = gr.Button("Grade")
|
|
|
|
| 129 |
grade_result = gr.Textbox(lines=15)
|
|
|
|
| 130 |
grade_btn.click(grade, grade_image, grade_result)
|
| 131 |
|
| 132 |
|
| 133 |
+
# β
THIS IS THE ONLY SAFE LAUNCH FOR HF SPACES
|
| 134 |
+
demo.queue().launch()
|