File size: 7,834 Bytes
f34dfa2 76f08bb f34dfa2 4c65203 cfea9cd f88822e 4c65203 f34dfa2 4c65203 f34dfa2 4c65203 f34dfa2 4c65203 f34dfa2 4c65203 f88822e 12dc835 4c65203 12dc835 4c65203 f34dfa2 cfea9cd f34dfa2 cfea9cd f34dfa2 cfea9cd 2a00ab9 cfea9cd 2a00ab9 cfea9cd 2a00ab9 f34dfa2 0fda7e1 f34dfa2 |
1 2 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
import gradio as gr
import spaces
import requests
import torch
from PIL import Image
from transformers import MllamaForConditionalGeneration, AutoProcessor
SCHEMA_DEFINITION= """{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"Issue_Description": {
"type": "string"
},
"Root_Cause_Analysis": {
"type": "object",
"properties": {
"LED_Analysis": {
"type": "object",
"properties": {
"Color": {
"type": "string"
},
"Pattern": {
"type": "string"
},
"Indicates": {
"type": "string"
}
},
"required": [
"Color",
"Pattern",
"Indicates"
]
},
"Error_Code": {
"type": "string"
},
"Possible_Cause": {
"type": "string"
}
},
"required": [
"LED_Analysis",
"Error_Code",
"Possible_Cause"
]
},
"Step_by_Step_Troubleshooting": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"Action": {
"type": "string"
},
"Details": {
"type": "string"
},
"Expected Outcome": {
"type": "string"
}
},
"required": [
"Action",
"Details",
"Expected Outcome"
]
},
{
"type": "object",
"properties": {
"Action": {
"type": "string"
},
"Details": {
"type": "string"
},
"Expected Outcome": {
"type": "string"
}
},
"required": [
"Action",
"Details",
"Expected Outcome"
]
},
{
"type": "object",
"properties": {
"Action": {
"type": "string"
},
"Details": {
"type": "string"
},
"Expected Outcome": {
"type": "string"
}
},
"required": [
"Action",
"Details",
"Expected Outcome"
]
},
{
"type": "object",
"properties": {
"Action": {
"type": "string"
},
"Details": {
"type": "string"
},
"Expected Outcome": {
"type": "string"
}
},
"required": [
"Action",
"Details",
"Expected Outcome"
]
}
]
},
"Recommended_Actions": {
"type": "object",
"properties": {
"Immediate_Action": {
"type": "string"
},
"If_Unresolved": {
"type": "string"
},
"Preventative_Measure": {
"type": "string"
}
},
"required": [
"Immediate_Action",
"If_Unresolved",
"Preventative_Measure"
]
}
},
"required": [
"Issue_Description",
"Root_Cause_Analysis",
"Step_by_Step_Troubleshooting",
"Recommended_Actions"
]
}"""
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
model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
model = MllamaForConditionalGeneration.from_pretrained(
model_id,
torch_dtype=torch.bfloat16,
device_map="auto",
)
processor = AutoProcessor.from_pretrained(model_id)
def extract_json_from_markdown(markdown_text):
"""Extract JSON or code block from markdown text."""
try:
# Find the start and end of the code block (with or without "json")
start_idx = markdown_text.find('```')
end_idx = markdown_text.find('```', start_idx + 3)
# If the block starts with '```json', skip the 'json' part
if markdown_text[start_idx:start_idx + 7] == '```json':
start_idx += len('```json')
else:
start_idx += len('```')
# Extract and clean up the code block (json or not)
json_str = markdown_text[start_idx:end_idx].strip()
# Try to load it as JSON
return json.loads(json_str)
except Exception as e:
print(f"Error extracting JSON: {e}")
return None
@spaces.GPU
def diagnose_router(image):
messages = [
{"role": "user", "content": [
{"type": "image"},
{"type": "text", "text": SYSTEM_INSTRUCTION}
]}
]
input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(image, input_text, return_tensors="pt").to(model.device)
# Generate the output from the model
output = model.generate(**inputs, max_new_tokens=300)
markdown_text = processor.decode(output[0])
# Extract JSON from the markdown text
result = extract_json_from_markdown(markdown_text)
print (result)
# Generate HTML content for structured display
html_output = f"""
<div style="font-family: Arial, sans-serif; color: #333;">
<h2>Router Diagnosis</h2>
<h3>Issue Description</h3>
<p><strong>{result['Issue_Description']}</strong></p>
<h3>Root Cause Analysis</h3>
<ul>
<li><strong>LED Color:</strong> {result['Root_Cause_Analysis']['LED_Analysis']['Color']}</li>
<li><strong>LED Pattern:</strong> {result['Root_Cause_Analysis']['LED_Analysis']['Pattern']}</li>
<li><strong>Indicates:</strong> {result['Root_Cause_Analysis']['LED_Analysis']['Indicates']}</li>
<li><strong>Error Code:</strong> {result['Root_Cause_Analysis']['Error_Code']}</li>
<li><strong>Possible Cause:</strong> {result['Root_Cause_Analysis']['Possible_Cause']}</li>
</ul>
<h3>Step-by-Step Troubleshooting</h3>
<ol>
"""
# Loop through each step in the troubleshooting process (now a list)
for step in result["Step_by_Step_Troubleshooting"]:
html_output += f"""
<li><strong>{step['Action']}</strong>: {step['Details']}<br/>
<em>Expected Outcome:</em> {step['Expected Outcome']}</li>
"""
# Adding the Recommended Actions section
html_output += f"""
</ol>
<h3>Recommended Actions</h3>
<ul>
<li><strong>Immediate Action:</strong> {result['Recommended_Actions']['Immediate_Action']}</li>
<li><strong>If Unresolved:</strong> {result['Recommended_Actions']['If_Unresolved']}</li>
<li><strong>Preventative Measure:</strong> {result['Recommended_Actions']['Preventative_Measure']}</li>
</ul>
</div>
"""
return html_output
# Gradio UI
interface = gr.Interface(
fn=diagnose_router,
inputs=gr.Image(type="pil", label="Upload an image of the faulty router"),
outputs=gr.HTML(),
title="Router Diagnosis",
description="Upload a photo of your router to receive a professional diagnosis and troubleshooting steps displayed in a structured, easy-to-read format."
)
# Launch the UI
interface.launch()
|