Spaces:
Running
Running
File size: 7,840 Bytes
a89d904 63afc1e 8dbfb64 c57ef4b a89d904 59b14ab 32fe223 59b14ab 56ac355 63afc1e 56ac355 63afc1e 788e25a 56ac355 bdd6f30 788e25a 56ac355 788e25a 56ac355 1b9a115 56ac355 68b31b0 56ac355 68b31b0 56ac355 68b31b0 56ac355 68b31b0 788e25a 68b31b0 bdd6f30 56ac355 bdd6f30 56ac355 a89d904 56ac355 1b9a115 56ac355 a89d904 c57ef4b a89d904 56ac355 1b9a115 56ac355 a89d904 56ac355 a89d904 59b14ab 2e45dd0 68b31b0 1b9a115 68b31b0 1b9a115 68b31b0 1b9a115 68b31b0 63afc1e 68b31b0 56ac355 68b31b0 59b14ab 63afc1e 2e45dd0 bdd6f30 56ac355 59b14ab a89d904 |
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 |
import requests
import gradio as gr
import os
import random
from datetime import datetime, timedelta
import plotly.express as px
import re
api_key = os.getenv("CEREBRAS_API_KEY")
url = "https://api.cerebras.ai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
SAMPLE_LOG = """
2025-04-12 10:01 UTC | 40.7N, 74.0W | Frequency: 14.5 GHz | Signal Strength: 85% | Message: Routine check, systems OK.
2025-04-12 10:02 UTC | 40.8N, 74.1W | Frequency: 14.7 GHz | Signal Strength: 60% | Message: Noise detected, possible interference.
2025-04-12 10:03 UTC | 40.9N, 74.2W | Frequency: 14.6 GHz | Signal Strength: 90% | Message: Emergency: Low battery alert.
"""
LOG_HISTORY = []
def generate_random_log():
base_time = datetime(2025, 4, 12, 10, 0)
entries = []
for i in range(3):
time = (base_time + timedelta(minutes=i)).strftime("%Y-%m-%d %H:%M UTC")
lat = round(random.uniform(40.0, 41.0), 1)
lon = round(random.uniform(73.0, 74.0), 1)
freq = round(random.uniform(14.0, 15.0), 1)
signal = random.randint(50, 100)
messages = [
"Routine check, systems OK.",
"Noise detected, possible interference.",
"Emergency: Low battery alert.",
"Signal stable, no issues.",
"Warning: Solar flare detected."
]
message = random.choice(messages)
entries.append(f"{time} | {lat}N, {lon}W | Frequency: {freq} GHz | Signal Strength: {signal}% | Message: {message}")
return "\n".join(entries)
def analyze_log(log_text):
if not log_text.strip():
return "Error: Please enter a log.", None, None
LOG_HISTORY.append(log_text)
data = {
"model": "llama-4-scout-17b-16e-instruct",
"messages": [
{
"role": "user",
"content": "Analyze this satellite radio log and summarize in bullet points. Include interference risk scores (0-100, low signal <70% = high risk >80, emergency = 90). Ensure frequencies in issues and details:\n- Issues (e.g., low signal, noise, interference with frequency, risk score)\n- High-priority messages (e.g., emergencies, warnings)\n- Key details (coordinates, times, frequencies, signal strengths)\nLog:\n" + log_text
}
],
"max_completion_tokens": 500,
"temperature": 0.5
}
try:
response = requests.post(url, json=data, headers=headers)
summary = response.json()["choices"][0]["message"]["content"]
html = "<div style='font-family:Roboto; color:black; background:white; padding:10px; border-radius:5px; border:1px solid #ff6200;'><h3>Analysis</h3><ul>"
for line in summary.split("\n"):
if "Issues:" in line:
html += "<li><b style='color:#ff6200;'>Issues:</b><ul>"
elif "High-priority messages:" in line:
html += "</ul><li><b style='color:#ff6200;'>Priority Alerts:</b><ul>"
elif "Key details:" in line:
html += "</ul><li><b style='color:#ff6200;'>Details:</b><ul>"
elif line.strip():
html += f"<li>{line.strip()}</li>"
html += "</ul></div>"
signals = []
times = []
for line in log_text.split("\n"):
if "Signal Strength" in line:
match = re.search(r"Signal Strength: (\d+)%", line)
if match:
signals.append(int(match.group(1)))
time_match = re.search(r"(\d{4}-\d{2}-\d{2} \d{2}:\d{2}) UTC", line)
if time_match:
times.append(time_match.group(1)[-5:])
fig = px.line(x=times, y=signals, labels={"x": "Time", "y": "Signal (%)"}, title="Signal Trend") if signals and len(signals) == len(times) else None
return summary, html, fig
except Exception as e:
return f"Error: API call failed - {str(e)}", None, None
def generate_alert(log_text):
if not log_text.strip():
return "Error: Please enter a log."
data = {
"model": "llama-4-scout-17b-16e-instruct",
"messages": [
{
"role": "user",
"content": "Generate an urgent satellite alert based on this log’s conditions (e.g., interference for noise/low signal, escalation for emergencies). Include frequency, time, coordinates:\nLog:\n" + log_text
}
],
"max_completion_tokens": 100,
"temperature": 0.7
}
try:
response = requests.post(url, json=data, headers=headers)
alert = response.json()["choices"][0]["message"]["content"]
return f"<div style='font-family:Roboto; background:#ff6200; color:white; padding:10px; border-radius:5px;'>{alert}</div>"
except Exception as e:
return f"Error: Alert failed - {str(e)}"
def compare_logs():
if len(LOG_HISTORY) < 2:
return "Error: Need at least two logs.", None
compare_text = "Previous log:\n" + LOG_HISTORY[-2] + "\nCurrent log:\n" + LOG_HISTORY[-1]
data = {
"model": "llama-4-scout-17b-16e-instruct",
"messages": [
{
"role": "user",
"content": "Compare these two satellite radio logs and summarize trends in bullet points (e.g., signal strength changes, frequency issues, new emergencies):\n" + compare_text
}
],
"max_completion_tokens": 400,
"temperature": 0.5
}
try:
response = requests.post(url, json=data, headers=headers)
comparison = response.json()["choices"][0]["message"]["content"]
html = "<div style='font-family:Roboto; color:black; background:white; padding:10px; border-radius:5px; border:1px solid #ff6200;'><h3>Comparison</h3><ul>"
for line in comparison.split("\n"):
if line.strip():
html += f"<li>{line.strip()}</li>"
html += "</ul></div>"
return comparison, html
except Exception as e:
return f"Error: Comparison failed - {str(e)}", None
def load_sample_log():
return SAMPLE_LOG
def clear_log():
return ""
css = """
body, .gradio-container { background: white; color: black; font-family: Roboto, sans-serif; }
button { background: #ff6200; color: white; border: none; padding: 8px 16px; border-radius: 5px; }
button:hover { background: #e55a00; }
.input-text, .output-text { background: white; color: black; border: 1px solid #ff6200; border-radius: 5px; }
h3 { color: #ff6200; }
.header, .subheader { color: #ff6200; text-align: center; }
"""
with gr.Blocks(css=css) as interface:
gr.Markdown("# Satellite Signal Log Analyzer", elem_classes="header")
gr.Markdown("Analyze logs for issues, alerts, and trends.", elem_classes="subheader")
log_input = gr.Textbox(lines=5, show_label=False, placeholder="Enter or generate a log...")
with gr.Row():
sample_button = gr.Button("Sample Log")
random_button = gr.Button("Random Log")
clear_button = gr.Button("Clear")
with gr.Row():
analyze_button = gr.Button("Analyze")
alert_button = gr.Button("Alert")
compare_button = gr.Button("Compare Logs")
output = gr.HTML(show_label=False)
plot_output = gr.Plot(show_label=False)
alert_output = gr.HTML(show_label=False)
compare_output = gr.HTML(show_label=False)
sample_button.click(fn=load_sample_log, outputs=log_input)
random_button.click(fn=generate_random_log, outputs=log_input)
clear_button.click(fn=clear_log, outputs=log_input)
analyze_button.click(fn=analyze_log, inputs=log_input, outputs=[output, output, plot_output])
alert_button.click(fn=generate_alert, inputs=log_input, outputs=alert_output)
compare_button.click(fn=compare_logs, outputs=[compare_output, compare_output])
interface.launch() |