AdarshRajDS commited on
Commit
f5b28b8
Β·
1 Parent(s): 1558d13

Fix HF Spaces runtime 1

Browse files
Files changed (1) hide show
  1. app.py +31 -47
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"❌ Error: {str(e)}"))
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"❌ Upload failed: {str(e)}"
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"❌ Error: {str(e)}", None
75
 
76
 
77
- # =====================================================
78
- # πŸ“ GRADE ANNOTATION
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"❌ Grading failed: {str(e)}"
92
 
93
 
94
- # =====================================================
95
  # 🎨 UI
96
- # =====================================================
97
- with gr.Blocks(
98
- title="Multimodal RAG Anatomy Tutor",
99
- analytics_enabled=False,
100
- show_api=False, # πŸ’₯ CRITICAL FIX
101
- ) as demo:
102
 
103
  gr.Markdown("# 🧠 Multimodal RAG Anatomy Tutor")
104
 
105
- # ================= CHAT =================
106
- with gr.Tab("πŸ’¬ Chat"):
107
  chatbot = gr.Chatbot(height=500)
108
  msg = gr.Textbox(placeholder="Ask anatomy question...")
 
109
 
110
- msg.submit(
111
- ask_question,
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
- # ================= VISUALIZE =================
125
- with gr.Tab("🧠 Visualize Anatomy"):
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
- # ================= GRADING =================
140
- with gr.Tab("πŸ“ Annotation Grading"):
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
- # βœ… HF SPACES LAUNCH MODE
150
- demo.queue(api_open=False).launch()
 
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()