yasserrmd commited on
Commit
4c65203
1 Parent(s): cfea9cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +209 -33
app.py CHANGED
@@ -1,48 +1,224 @@
1
  import gradio as gr
2
  import spaces
3
 
 
 
 
 
4
 
5
- @spaces.GPU
6
- def diagnose_router(image):
7
- result = {
8
- "Issue_Description": "Internet connectivity problem - The Internet LED is blinking amber.",
9
- "Root_Cause_Analysis": {
10
- "LED_Analysis": {
11
- "Color": "Amber",
12
- "Pattern": "Blinking",
13
- "Indicates": "The router is unable to establish a connection with the ISP."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  },
15
- "Error_Code": "WAN PPPoE Timeout",
16
- "Possible_Cause": "This error suggests the router cannot authenticate with the ISP's server, likely due to incorrect login credentials or an ISP-side issue."
 
 
 
 
 
 
 
17
  },
18
- "Step_by_Step_Troubleshooting": [
19
- {
20
- "Action": "Verify Ethernet Cable Connection",
21
- "Details": "Ensure that the Ethernet cable is securely plugged into the modem's LAN port and the router's WAN port.",
22
- "Expected Outcome": "Ethernet cable is securely connected, and the LED light remains stable."
 
 
 
23
  },
24
- {
25
- "Action": "Restart Both Modem and Router",
26
- "Details": "Unplug both the modem and router from their power sources, wait for 30 seconds, then plug them back in starting with the modem.",
27
- "Expected Outcome": "The router should reattempt to establish a connection with the ISP."
 
 
 
 
 
 
 
 
 
 
 
28
  },
29
- {
30
- "Action": "Verify ISP Login Credentials",
31
- "Details": "Access the router settings through its web interface and check the PPPoE login credentials (username and password) provided by your ISP.",
32
- "Expected Outcome": "Correct credentials should resolve the authentication issue."
33
  },
34
- {
35
- "Action": "Contact ISP Support",
36
- "Details": "If the above steps don't work, contact your ISP to check for any issues on their end such as outages or account problems.",
37
- "Expected Outcome": "ISP support will help resolve server authentication issues or provide further guidance."
38
  }
39
- ],
40
- "Recommended_Actions": {
41
- "Immediate_Action": "Reconnect cables and restart devices.",
42
- "If_Unresolved": "Check ISP login credentials and contact ISP if needed.",
43
- "Preventative_Measure": "Ensure router firmware is up-to-date to prevent known issues."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  }
 
 
 
 
 
 
45
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
  # Generate HTML content for structured display
48
  html_output = f"""
 
1
  import gradio as gr
2
  import spaces
3
 
4
+ import requests
5
+ 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_INNSTRUCTION="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"
163
+
164
+ model = MllamaForConditionalGeneration.from_pretrained(
165
+ model_id,
166
+ torch_dtype=torch.bfloat16,
167
+ device_map="auto",
168
+ )
169
+ processor = AutoProcessor.from_pretrained(model_id)
170
+
171
+
172
+ def extract_json_from_markdown(markdown_text):
173
+ """Extract JSON or code block from markdown text."""
174
+ try:
175
+ # Find the start and end of the code block (with or without "json")
176
+ start_idx = markdown_text.find('```')
177
+ end_idx = markdown_text.find('```', start_idx + 3)
178
+
179
+ # If the block starts with '```json', skip the 'json' part
180
+ if markdown_text[start_idx:start_idx + 7] == '```json':
181
+ start_idx += len('```json')
182
+ else:
183
+ start_idx += len('```')
184
+
185
+ # Extract and clean up the code block (json or not)
186
+ json_str = markdown_text[start_idx:end_idx].strip()
187
+
188
+ # Try to load it as JSON
189
+ return json.loads(json_str)
190
+ except Exception as e:
191
+ print(f"Error extracting JSON: {e}")
192
+ return None
193
+
194
+ @spaces.GPU
195
+ def diagnose_router(image):
196
+
197
+
198
+
199
+
200
+ messages = [
201
+ {"role": "system",
202
+ "content": [{"type": "text",
203
+ "text": SYSTEM_INNSTRUCTION}],
204
+ },
205
+ {"role": "user", "content": [
206
+ {"type": "image"},
207
+ {"type": "text", "text": "Analyze this router issue and provide the diagnosis."}
208
+ ]}
209
+ ]
210
+ input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
211
+ inputs = processor(image, input_text, return_tensors="pt").to(model.device)
212
+
213
+ # Generate the output from the model
214
+ output = model.generate(**inputs, max_new_tokens=300)
215
+ markdown_text = processor.decode(output[0])
216
+
217
+ # Extract JSON from the markdown text
218
+ result = extract_json_from_markdown(markdown_text)
219
+
220
+
221
+ print (result)
222
 
223
  # Generate HTML content for structured display
224
  html_output = f"""