yasserrmd commited on
Commit
146193f
1 Parent(s): bff4af1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -216
app.py CHANGED
@@ -6,157 +6,7 @@ import torch
6
  from PIL import Image
7
  from transformers import MllamaForConditionalGeneration, AutoProcessor
8
 
9
- SCHEMA_DEFINITION= """{
10
- "$schema": "http://json-schema.org/draft-04/schema#",
11
- "type": "object",
12
- "properties": {
13
- "Issue_Description": {
14
- "type": "string"
15
- },
16
- "Root_Cause_Analysis": {
17
- "type": "object",
18
- "properties": {
19
- "LED_Analysis": {
20
- "type": "object",
21
- "properties": {
22
- "Color": {
23
- "type": "string"
24
- },
25
- "Pattern": {
26
- "type": "string"
27
- },
28
- "Indicates": {
29
- "type": "string"
30
- }
31
- },
32
- "required": [
33
- "Color",
34
- "Pattern",
35
- "Indicates"
36
- ]
37
- },
38
- "Error_Code": {
39
- "type": "string"
40
- },
41
- "Possible_Cause": {
42
- "type": "string"
43
- }
44
- },
45
- "required": [
46
- "LED_Analysis",
47
- "Error_Code",
48
- "Possible_Cause"
49
- ]
50
- },
51
- "Step_by_Step_Troubleshooting": {
52
- "type": "array",
53
- "items": [
54
- {
55
- "type": "object",
56
- "properties": {
57
- "Action": {
58
- "type": "string"
59
- },
60
- "Details": {
61
- "type": "string"
62
- },
63
- "Expected Outcome": {
64
- "type": "string"
65
- }
66
- },
67
- "required": [
68
- "Action",
69
- "Details",
70
- "Expected Outcome"
71
- ]
72
- },
73
- {
74
- "type": "object",
75
- "properties": {
76
- "Action": {
77
- "type": "string"
78
- },
79
- "Details": {
80
- "type": "string"
81
- },
82
- "Expected Outcome": {
83
- "type": "string"
84
- }
85
- },
86
- "required": [
87
- "Action",
88
- "Details",
89
- "Expected Outcome"
90
- ]
91
- },
92
- {
93
- "type": "object",
94
- "properties": {
95
- "Action": {
96
- "type": "string"
97
- },
98
- "Details": {
99
- "type": "string"
100
- },
101
- "Expected Outcome": {
102
- "type": "string"
103
- }
104
- },
105
- "required": [
106
- "Action",
107
- "Details",
108
- "Expected Outcome"
109
- ]
110
- },
111
- {
112
- "type": "object",
113
- "properties": {
114
- "Action": {
115
- "type": "string"
116
- },
117
- "Details": {
118
- "type": "string"
119
- },
120
- "Expected Outcome": {
121
- "type": "string"
122
- }
123
- },
124
- "required": [
125
- "Action",
126
- "Details",
127
- "Expected Outcome"
128
- ]
129
- }
130
- ]
131
- },
132
- "Recommended_Actions": {
133
- "type": "object",
134
- "properties": {
135
- "Immediate_Action": {
136
- "type": "string"
137
- },
138
- "If_Unresolved": {
139
- "type": "string"
140
- },
141
- "Preventative_Measure": {
142
- "type": "string"
143
- }
144
- },
145
- "required": [
146
- "Immediate_Action",
147
- "If_Unresolved",
148
- "Preventative_Measure"
149
- ]
150
- }
151
- },
152
- "required": [
153
- "Issue_Description",
154
- "Root_Cause_Analysis",
155
- "Step_by_Step_Troubleshooting",
156
- "Recommended_Actions"
157
- ]
158
- }"""
159
- SYSTEM_INSTRUCTION="You are a router troubleshooter. Your job is to analyze the provided router image, identify potential issues such as faulty connections, incorrect LED patterns, or error codes, and offer precise troubleshooting steps. Based on your analysis, generate a detailed observation that includes a root cause analysis, step-by-step actions for resolving the issue, and recommended preventive measures. The output must be in JSON format as per the following schema, ensuring users can easily follow and implement the suggested solutions.\n" + SCHEMA_DEFINITION
160
 
161
 
162
  model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
@@ -181,27 +31,7 @@ def extract_assistant_reply(input_string):
181
  assistant_reply = input_string[start_index:].strip()
182
  return assistant_reply
183
 
184
- def extract_json_from_markdown(markdown_text):
185
- """Extract JSON or code block from markdown text."""
186
- try:
187
- # Find the start and end of the code block (with or without "json")
188
- start_idx = markdown_text.find('```')
189
- end_idx = markdown_text.find('```', start_idx + 3)
190
-
191
- # If the block starts with '```json', skip the 'json' part
192
- if markdown_text[start_idx:start_idx + 7] == '```json':
193
- start_idx += len('```json')
194
- else:
195
- start_idx += len('```')
196
 
197
- # Extract and clean up the code block (json or not)
198
- json_str = markdown_text[start_idx:end_idx].strip()
199
-
200
- # Try to load it as JSON
201
- return json.loads(json_str)
202
- except Exception as e:
203
- print(f"Error extracting JSON: {e}")
204
- return None
205
 
206
  @spaces.GPU
207
  def diagnose_router(image):
@@ -219,51 +49,6 @@ def diagnose_router(image):
219
  print(output)
220
  markdown_text = processor.decode(output[0])
221
  print(markdown_text)
222
- # Extract JSON from the markdown text
223
- #result = extract_json_from_markdown(markdown_text)
224
-
225
-
226
- #print (result)
227
-
228
- # Generate HTML content for structured display
229
- # html_output = f"""
230
- # <div style="font-family: Arial, sans-serif; color: #333;">
231
- # <h2>Router Diagnosis</h2>
232
- # <h3>Issue Description</h3>
233
- # <p><strong>{result['Issue_Description']}</strong></p>
234
-
235
- # <h3>Root Cause Analysis</h3>
236
- # <ul>
237
- # <li><strong>LED Color:</strong> {result['Root_Cause_Analysis']['LED_Analysis']['Color']}</li>
238
- # <li><strong>LED Pattern:</strong> {result['Root_Cause_Analysis']['LED_Analysis']['Pattern']}</li>
239
- # <li><strong>Indicates:</strong> {result['Root_Cause_Analysis']['LED_Analysis']['Indicates']}</li>
240
- # <li><strong>Error Code:</strong> {result['Root_Cause_Analysis']['Error_Code']}</li>
241
- # <li><strong>Possible Cause:</strong> {result['Root_Cause_Analysis']['Possible_Cause']}</li>
242
- # </ul>
243
-
244
- # <h3>Step-by-Step Troubleshooting</h3>
245
- # <ol>
246
- # """
247
-
248
- # # Loop through each step in the troubleshooting process (now a list)
249
- # for step in result["Step_by_Step_Troubleshooting"]:
250
- # html_output += f"""
251
- # <li><strong>{step['Action']}</strong>: {step['Details']}<br/>
252
- # <em>Expected Outcome:</em> {step['Expected Outcome']}</li>
253
- # """
254
-
255
- # # Adding the Recommended Actions section
256
- # html_output += f"""
257
- # </ol>
258
-
259
- # <h3>Recommended Actions</h3>
260
- # <ul>
261
- # <li><strong>Immediate Action:</strong> {result['Recommended_Actions']['Immediate_Action']}</li>
262
- # <li><strong>If Unresolved:</strong> {result['Recommended_Actions']['If_Unresolved']}</li>
263
- # <li><strong>Preventative Measure:</strong> {result['Recommended_Actions']['Preventative_Measure']}</li>
264
- # </ul>
265
- # </div>
266
- # """
267
 
268
  markdown_text=extract_assistant_reply(markdown_text)
269
  html_output = markdown.markdown(markdown_text)
 
6
  from PIL import Image
7
  from transformers import MllamaForConditionalGeneration, AutoProcessor
8
 
9
+ SYSTEM_INSTRUCTION="You are a medical report interpreter. Your task is to analyze the provided medical reports, identify key medical terms, diagnoses, or abnormalities, and provide a clear interpretation. Based on your analysis, generate a detailed summary that includes an explanation of the findings, recommended actions, and any additional insights for the patient or healthcare provider. Ensure your output is structured and easily understandable for both professionals and non-professionals."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
 
12
  model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
 
31
  assistant_reply = input_string[start_index:].strip()
32
  return assistant_reply
33
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
 
 
 
 
 
 
 
 
35
 
36
  @spaces.GPU
37
  def diagnose_router(image):
 
49
  print(output)
50
  markdown_text = processor.decode(output[0])
51
  print(markdown_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  markdown_text=extract_assistant_reply(markdown_text)
54
  html_output = markdown.markdown(markdown_text)