AdarshRajDS commited on
Commit
b04d4d4
Β·
1 Parent(s): b4517e4

Fix HF Spaces runtime 3

Browse files
Files changed (1) hide show
  1. app.py +31 -49
app.py CHANGED
@@ -12,21 +12,18 @@ BACKEND_URL = "https://adarshds-thesisbackend.hf.space"
12
  # -----------------------------
13
  def ask_question(message, history):
14
  try:
15
- response = requests.post(
16
  f"{BACKEND_URL}/rag/ask",
17
  json={"question": message},
18
  timeout=120,
19
  )
 
20
 
21
- data = response.json()
22
-
23
- images_md = ""
24
  for img in data.get("images", []):
25
- images_md += f"\n\n![img]({BACKEND_URL}/{img})"
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 PDF
39
  # -----------------------------
40
  def upload_pdf(file):
41
  try:
42
- response = requests.post(
43
  f"{BACKEND_URL}/upload/pdf",
44
  files={"file": file},
45
  timeout=300,
46
  )
47
- return response.json()["message"]
48
-
49
  except Exception as e:
50
- return f"❌ {str(e)}"
51
 
52
 
53
  # -----------------------------
@@ -55,19 +51,17 @@ def upload_pdf(file):
55
  # -----------------------------
56
  def visualize(image, question):
57
  try:
58
- response = requests.post(
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 f"❌ {str(e)}", None
71
 
72
 
73
  # -----------------------------
@@ -75,60 +69,48 @@ def visualize(image, question):
75
  # -----------------------------
76
  def grade(image):
77
  try:
78
- response = requests.post(
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 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
-
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
- 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()
 
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![img]({BACKEND_URL}/{img})"
 
 
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()