anaghanagesh commited on
Commit
dd2a5d2
·
verified ·
1 Parent(s): dd16a13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +232 -45
app.py CHANGED
@@ -1,69 +1,256 @@
1
- def create_molecule_viewer(smiles):
 
 
2
 
3
- return f"""
4
- <iframe
5
- width="100%"
6
- height="500"
7
- frameborder="0"
8
 
9
- srcdoc='
 
 
 
 
10
 
11
- <html>
 
 
12
 
13
- <head>
14
 
15
- <script src="https://3Dmol.org/build/3Dmol-min.js"></script>
16
 
17
- </head>
18
 
19
- <body style="margin:0">
 
 
 
 
 
 
 
20
 
21
- <div
22
- id="viewer"
23
- style="
24
- width:100vw;
25
- height:100vh;
26
- position:relative;
27
- background:white;
28
- ">
29
- </div>
30
 
31
- <script>
 
32
 
33
- let viewer = $3Dmol.createViewer(
34
- document.getElementById("viewer"),
 
35
  {{
36
  backgroundColor: "white"
37
  }}
38
  );
39
 
40
- viewer.addModel(
41
- "{smiles}",
42
- "smi"
43
- );
44
 
45
- viewer.setStyle(
46
- {{}},
47
- {{
48
- stick: {{}},
49
- sphere: {{
50
- scale:0.3
51
- }}
 
52
  }}
53
- );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
- viewer.zoomTo();
 
 
56
 
57
- viewer.spin(true);
 
 
 
58
 
59
- viewer.render();
 
 
 
60
 
61
- </script>
 
 
62
 
63
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- </html>
66
 
67
- '>
68
- </iframe>
69
- """
 
1
+ import gradio as gr
2
+
3
+ from agents import run_pipeline
4
 
 
 
 
 
 
5
 
6
+ # ====================================
7
+ # 3D MOLECULE VIEWER
8
+ # ====================================
9
+
10
+ def create_molecule_viewer(smiles):
11
 
12
+ html = f"""
13
+ <html>
14
+ <head>
15
 
16
+ <script src="https://3Dmol.org/build/3Dmol-min.js"></script>
17
 
18
+ </head>
19
 
20
+ <body style="margin:0; background:white;">
21
 
22
+ <div
23
+ id="container"
24
+ style="
25
+ width:100%;
26
+ height:500px;
27
+ position:relative;
28
+ ">
29
+ </div>
30
 
31
+ <script>
 
 
 
 
 
 
 
 
32
 
33
+ let element =
34
+ document.getElementById("container");
35
 
36
+ let viewer =
37
+ $3Dmol.createViewer(
38
+ element,
39
  {{
40
  backgroundColor: "white"
41
  }}
42
  );
43
 
44
+ viewer.addModel(
45
+ `{smiles}`,
46
+ "smi"
47
+ );
48
 
49
+ viewer.setStyle(
50
+ {{}},
51
+ {{
52
+ stick: {{
53
+ radius:0.18
54
+ }},
55
+ sphere: {{
56
+ scale:0.30
57
  }}
58
+ }}
59
+ );
60
+
61
+ viewer.zoomTo();
62
+
63
+ viewer.spin(true);
64
+
65
+ viewer.render();
66
+
67
+ </script>
68
+
69
+ </body>
70
+ </html>
71
+ """
72
+
73
+ return html
74
+
75
+
76
+ # ====================================
77
+ # MAIN ANALYSIS FUNCTION
78
+ # ====================================
79
+
80
+ def analyze(symptoms, followup, audio):
81
+
82
+ try:
83
+
84
+ combined_input = ""
85
+
86
+ # TEXT INPUT
87
+ if symptoms and symptoms.strip():
88
+
89
+ combined_input += symptoms
90
+
91
+ # FOLLOW-UP INPUT
92
+ if followup and followup.strip():
93
+
94
+ combined_input += (
95
+ " " + followup
96
+ )
97
+
98
+ # AUDIO SUPPORT
99
+ result = run_pipeline(
100
+ combined_input,
101
+ audio
102
+ )
103
+
104
+ response = result["response"]
105
+
106
+ pubmed = "\n".join(
107
+ result["pubmed"]
108
+ )
109
+
110
+ molecule_info = ""
111
+
112
+ molecule_html = ""
113
+
114
+ # MOLECULE DISPLAY
115
+ if result["molecules"]:
116
+
117
+ molecule = result["molecules"][0]
118
+
119
+ molecule_info = f"""
120
+ Drug: {molecule['name']}
121
+
122
+ SMILES:
123
+ {molecule['smiles']}
124
+
125
+ Molecular Weight:
126
+ {molecule['molecular_weight']}
127
+ """
128
+
129
+ molecule_html = create_molecule_viewer(
130
+ molecule["smiles"]
131
+ )
132
+
133
+ return (
134
+ response,
135
+ pubmed,
136
+ molecule_info,
137
+ molecule_html
138
+ )
139
+
140
+ except Exception as e:
141
+
142
+ print("APP ERROR:", e)
143
+
144
+ return (
145
+ f"Error: {str(e)}",
146
+ "",
147
+ "",
148
+ ""
149
+ )
150
+
151
+
152
+ # ====================================
153
+ # CUSTOM CSS
154
+ # ====================================
155
+
156
+ custom_css = """
157
+
158
+ body {
159
+
160
+ background:
161
+ linear-gradient(
162
+ to right,
163
+ #141e30,
164
+ #243b55
165
+ );
166
+
167
+ color:white;
168
+ }
169
+
170
+ .gradio-container {
171
+
172
+ animation:
173
+ fadeIn 1.5s ease-in-out;
174
+ }
175
+
176
+ textarea {
177
+
178
+ font-size:16px !important;
179
+ }
180
+
181
+ """
182
+
183
+
184
+ # ====================================
185
+ # UI
186
+ # ====================================
187
+
188
+ with gr.Blocks(
189
+ theme=gr.themes.Soft(),
190
+ css=custom_css
191
+ ) as demo:
192
+
193
+ gr.Markdown(
194
+ """
195
+ # 🧬 AI Biomedical Assistant
196
+
197
+ Real-Time Retrieval-Augmented Biomedical Intelligence System
198
+ """
199
+ )
200
+
201
+ symptoms_input = gr.Textbox(
202
+ label="🩺 Symptoms",
203
+ placeholder="Example: chest pain, nausea, fever..."
204
+ )
205
+
206
+ followup_input = gr.Textbox(
207
+ label="💬 Follow-Up Information",
208
+ placeholder="Example: duration, severity..."
209
+ )
210
+
211
+ audio_input = gr.Audio(
212
+ sources=["microphone"],
213
+ type="filepath",
214
+ label="🎙️ Record Symptoms"
215
+ )
216
+
217
+ analyze_btn = gr.Button(
218
+ "🧠 Analyze"
219
+ )
220
 
221
+ diagnosis_output = gr.Markdown(
222
+ label="🧠 Biomedical Report"
223
+ )
224
 
225
+ pubmed_output = gr.Textbox(
226
+ label="📄 PubMed IDs",
227
+ lines=5
228
+ )
229
 
230
+ molecule_output = gr.Textbox(
231
+ label="🧪 Drug Information",
232
+ lines=8
233
+ )
234
 
235
+ molecule_viewer = gr.HTML(
236
+ label="🧬 Animated 3D Molecule"
237
+ )
238
 
239
+ analyze_btn.click(
240
+ fn=analyze,
241
+ inputs=[
242
+ symptoms_input,
243
+ followup_input,
244
+ audio_input
245
+ ],
246
+ outputs=[
247
+ diagnosis_output,
248
+ pubmed_output,
249
+ molecule_output,
250
+ molecule_viewer
251
+ ]
252
+ )
253
 
254
+ demo.queue()
255
 
256
+ demo.launch()