Spaces:
Sleeping
Sleeping
File size: 4,252 Bytes
6ec8da5 61857b5 6ec8da5 52d3d60 f508a3a 52253d3 b1dbecf 52d3d60 b1dbecf 52d3d60 b1dbecf 52d3d60 61857b5 b1dbecf 61857b5 b1dbecf 52253d3 b1dbecf 52253d3 b1dbecf 52253d3 b1dbecf 52d3d60 f508a3a 52d3d60 b1dbecf f508a3a 52253d3 f508a3a b1dbecf 6ec8da5 b1dbecf 6ec8da5 f508a3a 6ec8da5 52d3d60 52253d3 f508a3a b1dbecf f508a3a b1dbecf 52d3d60 b1dbecf 52253d3 |
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 |
import streamlit as st
import speedtest
import time
import pandas as pd
from datetime import datetime
import requests
import os
# ========== PAGE CONFIG ==========
st.set_page_config(page_title="AI-Powered Speed Test", layout="centered")
# Dark theme
st.markdown("""
<style>
body { background-color: #111827; color: #f9fafb; }
.stButton>button { background-color: #2563eb; color: white; border-radius: 10px; }
</style>
""", unsafe_allow_html=True)
st.title("πΆ Internet Speed Test + π§ AI Network Advisor")
# ========== PUBLIC IP ==========
def get_public_ip():
try:
res = requests.get("https://api.ipify.org?format=json", timeout=5)
return res.json().get("ip", "Unavailable")
except:
return "Unavailable"
st.markdown(f"π **Your Public IP:** `{get_public_ip()}`")
# ========== SESSION STATE ==========
if "history" not in st.session_state:
st.session_state.history = []
if "last_test" not in st.session_state:
st.session_state.last_test = 0
# ========== GROQ AI FUNCTION ==========
def analyze_with_groq(download, upload, latency):
api_key = os.getenv("speedtest")
if not api_key:
return "β οΈ GROQ API key not set."
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
prompt = (
f"My internet speed is:\n"
f"π₯ Download: {download} Mbps\n"
f"π€ Upload : {upload} Mbps\n"
f"β±οΈ Latency : {latency} sec\n\n"
"What online tasks (e.g., Zoom, gaming, streaming) can I perform smoothly with this connection?"
)
payload = {
"model": "llama3-8b-8192",
"messages": [{"role": "user", "content": prompt}]
}
try:
res = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
if res.status_code == 200:
return res.json()["choices"][0]["message"]["content"]
else:
return f"β GROQ Error: {res.status_code} - {res.text}"
except Exception as e:
return f"β Failed to connect to GROQ: {e}"
# ========== SPEED TEST FUNCTION ==========
def run_speed_test():
stt = speedtest.Speedtest()
stt.get_best_server()
start = time.time()
download = stt.download()
upload = stt.upload()
end = time.time()
download_mbps = round(download / 1_000_000, 2)
upload_mbps = round(upload / 1_000_000, 2)
test_time_sec = round(end - start, 2)
result = {
"Timestamp": datetime.now().strftime("%H:%M:%S"),
"Download (Mbps)": download_mbps,
"Upload (Mbps)": upload_mbps,
"Time (s)": test_time_sec,
}
st.session_state.history.append(result)
st.session_state.last_test = time.time()
st.success("β
Test complete!")
st.markdown(f"**π₯ Download:** `{download_mbps} Mbps`")
st.markdown(f"**π€ Upload :** `{upload_mbps} Mbps`")
st.markdown(f"**β±οΈ Time :** `{test_time_sec} sec`")
st.markdown("### π§ GROQ AI Evaluation:")
with st.spinner("Asking GROQ AI..."):
groq_response = analyze_with_groq(download_mbps, upload_mbps, test_time_sec)
st.info(groq_response)
# ========== RETEST SETUP ==========
interval = st.selectbox("β±οΈ Retest every (minutes)", [0, 1, 5, 10, 15, 30, 60], index=0)
auto_retest = interval > 0
# ========== RUN TEST BUTTON ==========
if st.button("βΆοΈ Start Speed Test"):
with st.spinner("Running speed test..."):
try:
run_speed_test()
except Exception as e:
st.error(f"β Speed test failed: {e}")
# ========== AUTO RETEST ==========
if auto_retest:
now = time.time()
if now - st.session_state.last_test > interval * 60:
with st.spinner("Auto-retesting..."):
try:
run_speed_test()
except Exception as e:
st.error(f"β Auto test failed: {e}")
# ========== HISTORY ==========
if st.session_state.history:
st.markdown("---")
st.subheader("π Test History")
df = pd.DataFrame(st.session_state.history)
st.line_chart(df[["Download (Mbps)", "Upload (Mbps)"]])
with st.expander("π Full results"):
st.dataframe(df, use_container_width=True) |