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

Fix HF Spaces runtime

Browse files
Files changed (1) hide show
  1. app.py +90 -64
app.py CHANGED
@@ -1,109 +1,132 @@
1
  import os
2
-
3
- import os
4
-
5
- os.environ["GRADIO_SERVER_NAME"] = "0.0.0.0"
6
- os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
7
- os.environ["GRADIO_ALLOW_FLAGGING"] = "never"
8
-
9
-
10
-
11
  import gradio as gr
12
  import requests
13
 
 
 
 
14
  BACKEND_URL = "https://adarshds-thesisbackend.hf.space"
15
 
16
 
17
- # -----------------------------
18
  # πŸ’¬ RAG CHAT
19
- # -----------------------------
20
  def ask_question(message, history):
21
- response = requests.post(
22
- f"{BACKEND_URL}/rag/ask",
23
- json={"question": message},
24
- timeout=120,
25
- )
 
 
 
26
 
27
- data = response.json()
 
 
28
 
29
- images = ""
30
- for img in data.get("images", []):
31
- images += f"\n\n![img]({BACKEND_URL}/{img})"
32
 
33
- answer = data["answer"] + images
 
34
 
35
- history.append((message, answer))
36
- return "", history
 
37
 
38
 
39
- # -----------------------------
40
  # πŸ“„ UPLOAD PDF
41
- # -----------------------------
42
  def upload_pdf(file):
43
- response = requests.post(
44
- f"{BACKEND_URL}/upload/pdf",
45
- files={"file": file},
46
- timeout=300,
47
- )
 
 
48
 
49
- return response.json()["message"]
 
50
 
51
 
52
- # -----------------------------
53
  # 🧠 VISUALIZE
54
- # -----------------------------
55
  def visualize(image, question):
56
- response = requests.post(
57
- f"{BACKEND_URL}/visualize",
58
- files={"image": image},
59
- data={"question": question},
60
- timeout=120,
61
- )
 
 
 
62
 
63
- data = response.json()
 
 
 
64
 
65
- return data["answer"], f"{BACKEND_URL}/{data['output_image']}"
 
66
 
67
 
68
- # -----------------------------
69
- # πŸ“ GRADE
70
- # -----------------------------
71
  def grade(image):
72
- response = requests.post(
73
- f"{BACKEND_URL}/grade-annotation/",
74
- files={"file": image},
75
- timeout=120,
76
- )
 
77
 
78
- return response.json()["result"]
79
 
 
 
80
 
81
- # ==================================================
82
- # 🎨 UI
83
- # ==================================================
84
 
85
- with gr.Blocks(analytics_enabled=False) as demo:
 
 
 
 
 
 
 
86
 
87
  gr.Markdown("# 🧠 Multimodal RAG Anatomy Tutor")
88
 
89
- # πŸ’¬ CHAT TAB
90
- with gr.Tab("Chat"):
91
  chatbot = gr.Chatbot(height=500)
92
  msg = gr.Textbox(placeholder="Ask anatomy question...")
93
- msg.submit(ask_question, [msg, chatbot], [msg, chatbot])
94
 
95
- # πŸ“„ UPLOAD TAB
96
- with gr.Tab("Upload PDF"):
 
 
 
 
 
 
97
  file = gr.File(file_types=[".pdf"])
98
  upload_btn = gr.Button("Upload & Ingest")
99
  upload_output = gr.Textbox()
 
100
  upload_btn.click(upload_pdf, file, upload_output)
101
 
102
- # 🧠 VISUAL TAB
103
- with gr.Tab("Visualize Anatomy"):
104
  vis_image = gr.Image(type="filepath")
105
  vis_question = gr.Textbox(label="Ask about the structure")
106
  vis_btn = gr.Button("Visualize")
 
107
  vis_answer = gr.Textbox()
108
  vis_output = gr.Image()
109
 
@@ -113,12 +136,15 @@ with gr.Blocks(analytics_enabled=False) as demo:
113
  [vis_answer, vis_output],
114
  )
115
 
116
- # πŸ“ GRADING TAB
117
- with gr.Tab("Annotation Grading"):
118
  grade_image = gr.Image(type="filepath")
119
  grade_btn = gr.Button("Grade")
 
120
  grade_result = gr.Textbox(lines=15)
 
121
  grade_btn.click(grade, grade_image, grade_result)
122
 
123
 
124
- demo.queue().launch(server_name="0.0.0.0", server_port=7860)
 
 
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(
17
+ f"{BACKEND_URL}/rag/ask",
18
+ json={"question": message},
19
+ timeout=120,
20
+ )
21
+
22
+ data = response.json()
23
 
24
+ images_md = ""
25
+ for img in data.get("images", []):
26
+ images_md += f"\n\n![img]({BACKEND_URL}/{img})"
27
 
28
+ answer = data["answer"] + images_md
 
 
29
 
30
+ history.append((message, answer))
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(
44
+ f"{BACKEND_URL}/upload/pdf",
45
+ files={"file": file},
46
+ timeout=300,
47
+ )
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(
60
+ f"{BACKEND_URL}/visualize",
61
+ files={"image": open(image, "rb")},
62
+ data={"question": question},
63
+ timeout=120,
64
+ )
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(
83
+ f"{BACKEND_URL}/grade-annotation/",
84
+ files={"file": open(image, "rb")},
85
+ timeout=120,
86
+ )
87
 
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
  [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()