File size: 2,610 Bytes
c9d2002
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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()