networkchatbot / app.py
qasimgill's picture
Update app.py
c9d2002 verified
# Install compatible versions
# !pip install --upgrade torch torchvision torchaudio transformers speedtest-cli sentencepiece accelerate gradio
import gradio as gr
from transformers import pipeline
import speedtest
import torch
# Clear GPU cache
torch.cuda.empty_cache()
# Load AI model
model_name = "google/flan-t5-large"
chatbot = pipeline("text2text-generation", model=model_name, device=0)
# Function to check network speed and troubleshoot
def check_network_speed(user_issue):
if not user_issue.strip():
return "⚠️ **Please enter a valid network issue!**"
# Show loading indicator
yield "⏳ **Analyzing network issue... Running speed test...**"
# Run speed test
st_obj = speedtest.Speedtest()
download_speed = st_obj.download() / 1_000_000 # Convert to Mbps
upload_speed = st_obj.upload() / 1_000_000 # Convert to Mbps
ping_latency = st_obj.results.ping
# **Prompt for AI model**
prompt = f"""
A user is experiencing a network issue: "{user_issue}"
Network Speed Test Results:
- Download Speed: {download_speed:.2f} Mbps
- Upload Speed: {upload_speed:.2f} Mbps
- Ping: {ping_latency:.2f} ms
Provide exactly 5 different troubleshooting steps to help the user.
Each step must be unique, actionable, and relevant to the problem.
Avoid repeating steps or giving generic advice like "Check your internet".
"""
# Show processing status
yield f"⏳ **Analyzing speed test results... Generating troubleshooting steps...**"
# Generate AI response
response = chatbot(prompt, max_length=250, do_sample=True, temperature=0.5, num_return_sequences=1)
# Final response
yield (
f"πŸ”½ **Download Speed:** {download_speed:.2f} Mbps\n"
f"πŸ”Ό **Upload Speed:** {upload_speed:.2f} Mbps\n"
f"πŸ“Ά **Ping Latency:** {ping_latency:.2f} ms\n\n"
f"### πŸ” Troubleshooting Steps:\n{response[0]['generated_text']}"
)
# Gradio UI with compact design
with gr.Blocks(theme=gr.themes.Base()) as iface: # πŸ”Ή Removes Gradio footer
gr.Markdown("## πŸ“‘ Network Troubleshooting Chatbot")
gr.Markdown("Enter your network issue, and the AI will diagnose the problem with a speed test and troubleshooting steps.")
user_input = gr.Textbox(placeholder="Describe your network problem...", label="πŸ” Enter Your Network Issue")
diagnose_button = gr.Button("Diagnose", variant="primary") # πŸ”Ή Ensures proper button display
output = gr.Markdown() # πŸ”Ή Output area
diagnose_button.click(fn=check_network_speed, inputs=user_input, outputs=output)
# Run Gradio app
iface.launch()