Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,224 @@
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
},
|
15 |
-
"
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
},
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
23 |
},
|
24 |
-
{
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
},
|
29 |
-
{
|
30 |
-
|
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 |
-
|
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 |
-
|
41 |
-
"
|
42 |
-
"
|
43 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"""
|