Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -17,6 +17,9 @@ SAMPLE_LOG = """
|
|
17 |
2025-04-12 10:03 UTC | 40.9N, 74.2W | Frequency: 14.6 GHz | Signal Strength: 90% | Message: Emergency: Low battery alert.
|
18 |
"""
|
19 |
|
|
|
|
|
|
|
20 |
def generate_random_log():
|
21 |
base_time = datetime(2025, 4, 12, 10, 0)
|
22 |
entries = []
|
@@ -30,13 +33,48 @@ def generate_random_log():
|
|
30 |
"Routine check, systems OK.",
|
31 |
"Noise detected, possible interference.",
|
32 |
"Emergency: Low battery alert.",
|
33 |
-
"Signal stable, no issues."
|
|
|
34 |
]
|
35 |
message = random.choice(messages)
|
36 |
entries.append(f"{time} | {lat}N, {lon}W | Frequency: {freq} GHz | Signal Strength: {signal}% | Message: {message}")
|
37 |
return "\n".join(entries)
|
38 |
|
39 |
def analyze_log(log_text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
if not log_text.strip():
|
41 |
return "Error: Please enter a log."
|
42 |
data = {
|
@@ -44,17 +82,45 @@ def analyze_log(log_text):
|
|
44 |
"messages": [
|
45 |
{
|
46 |
"role": "user",
|
47 |
-
"content": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
}
|
49 |
],
|
50 |
-
"max_completion_tokens":
|
51 |
"temperature": 0.5
|
52 |
}
|
53 |
try:
|
54 |
response = requests.post(url, json=data, headers=headers)
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
except Exception as e:
|
57 |
-
return f"Error:
|
58 |
|
59 |
def load_sample_log():
|
60 |
return SAMPLE_LOG
|
@@ -64,17 +130,24 @@ def clear_log():
|
|
64 |
|
65 |
with gr.Blocks(theme="huggingface/dark") as interface:
|
66 |
gr.Markdown("# Satellite Signal Log Analyzer")
|
67 |
-
gr.Markdown("
|
68 |
log_input = gr.Textbox(lines=5, label="Satellite Radio Log")
|
69 |
with gr.Row():
|
70 |
sample_button = gr.Button("Load Sample Log")
|
71 |
random_button = gr.Button("Generate Random Log")
|
72 |
clear_button = gr.Button("Clear Log")
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
75 |
sample_button.click(fn=load_sample_log, outputs=log_input)
|
76 |
random_button.click(fn=generate_random_log, outputs=log_input)
|
77 |
clear_button.click(fn=clear_log, outputs=log_input)
|
78 |
-
analyze_button.click(fn=analyze_log, inputs=log_input, outputs=output)
|
|
|
|
|
79 |
|
80 |
interface.launch()
|
|
|
17 |
2025-04-12 10:03 UTC | 40.9N, 74.2W | Frequency: 14.6 GHz | Signal Strength: 90% | Message: Emergency: Low battery alert.
|
18 |
"""
|
19 |
|
20 |
+
# Store logs for comparison
|
21 |
+
LOG_HISTORY = []
|
22 |
+
|
23 |
def generate_random_log():
|
24 |
base_time = datetime(2025, 4, 12, 10, 0)
|
25 |
entries = []
|
|
|
33 |
"Routine check, systems OK.",
|
34 |
"Noise detected, possible interference.",
|
35 |
"Emergency: Low battery alert.",
|
36 |
+
"Signal stable, no issues.",
|
37 |
+
"Warning: Solar flare detected."
|
38 |
]
|
39 |
message = random.choice(messages)
|
40 |
entries.append(f"{time} | {lat}N, {lon}W | Frequency: {freq} GHz | Signal Strength: {signal}% | Message: {message}")
|
41 |
return "\n".join(entries)
|
42 |
|
43 |
def analyze_log(log_text):
|
44 |
+
if not log_text.strip():
|
45 |
+
return "Error: Please enter a log.", None
|
46 |
+
LOG_HISTORY.append(log_text)
|
47 |
+
data = {
|
48 |
+
"model": "llama-4-scout-17b-16e-instruct",
|
49 |
+
"messages": [
|
50 |
+
{
|
51 |
+
"role": "user",
|
52 |
+
"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
|
53 |
+
}
|
54 |
+
],
|
55 |
+
"max_completion_tokens": 500,
|
56 |
+
"temperature": 0.5
|
57 |
+
}
|
58 |
+
try:
|
59 |
+
response = requests.post(url, json=data, headers=headers)
|
60 |
+
summary = response.json()["choices"][0]["message"]["content"]
|
61 |
+
# Format as HTML table
|
62 |
+
html = "<div style='font-family:Arial;'><h3>Analysis Summary</h3><ul>"
|
63 |
+
for line in summary.split("\n"):
|
64 |
+
if "Issues:" in line:
|
65 |
+
html += "<li><b style='color:red;'>Issues:</b><ul>"
|
66 |
+
elif "High-priority messages:" in line:
|
67 |
+
html += "</ul><li><b style='color:orange;'>High-priority messages:</b><ul>"
|
68 |
+
elif "Key details:" in line:
|
69 |
+
html += "</ul><li><b style='color:lightblue;'>Key details:</b><ul>"
|
70 |
+
elif line.strip():
|
71 |
+
html += f"<li>{line.strip()}</li>"
|
72 |
+
html += "</ul></div>"
|
73 |
+
return summary, html
|
74 |
+
except Exception as e:
|
75 |
+
return f"Error: API call failed - {str(e)}", None
|
76 |
+
|
77 |
+
def generate_alert(log_text):
|
78 |
if not log_text.strip():
|
79 |
return "Error: Please enter a log."
|
80 |
data = {
|
|
|
82 |
"messages": [
|
83 |
{
|
84 |
"role": "user",
|
85 |
+
"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
|
86 |
+
}
|
87 |
+
],
|
88 |
+
"max_completion_tokens": 100,
|
89 |
+
"temperature": 0.7
|
90 |
+
}
|
91 |
+
try:
|
92 |
+
response = requests.post(url, json=data, headers=headers)
|
93 |
+
alert = response.json()["choices"][0]["message"]["content"]
|
94 |
+
return f"<div style='background:red; color:white; padding:10px; border-radius:5px;'>{alert}</div>"
|
95 |
+
except Exception as e:
|
96 |
+
return f"Error: Alert failed - {str(e)}"
|
97 |
+
|
98 |
+
def compare_logs():
|
99 |
+
if len(LOG_HISTORY) < 2:
|
100 |
+
return "Error: Need at least two logs.", None
|
101 |
+
compare_text = "Previous log:\n" + LOG_HISTORY[-2] + "\nCurrent log:\n" + LOG_HISTORY[-1]
|
102 |
+
data = {
|
103 |
+
"model": "llama-4-scout-17b-16e-instruct",
|
104 |
+
"messages": [
|
105 |
+
{
|
106 |
+
"role": "user",
|
107 |
+
"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
|
108 |
}
|
109 |
],
|
110 |
+
"max_completion_tokens": 200,
|
111 |
"temperature": 0.5
|
112 |
}
|
113 |
try:
|
114 |
response = requests.post(url, json=data, headers=headers)
|
115 |
+
comparison = response.json()["choices"][0]["message"]["content"]
|
116 |
+
html = "<div style='font-family:Arial;'><h3>Log Comparison</h3><ul>"
|
117 |
+
for line in comparison.split("\n"):
|
118 |
+
if line.strip():
|
119 |
+
html += f"<li>{line.strip()}</li>"
|
120 |
+
html += "</ul></div>"
|
121 |
+
return comparison, html
|
122 |
except Exception as e:
|
123 |
+
return f"Error: Comparison failed - {str(e)}", None
|
124 |
|
125 |
def load_sample_log():
|
126 |
return SAMPLE_LOG
|
|
|
130 |
|
131 |
with gr.Blocks(theme="huggingface/dark") as interface:
|
132 |
gr.Markdown("# Satellite Signal Log Analyzer")
|
133 |
+
gr.Markdown("Analyze satellite radio logs for issues, alerts, and trends using Llama 4 and Cerebras.")
|
134 |
log_input = gr.Textbox(lines=5, label="Satellite Radio Log")
|
135 |
with gr.Row():
|
136 |
sample_button = gr.Button("Load Sample Log")
|
137 |
random_button = gr.Button("Generate Random Log")
|
138 |
clear_button = gr.Button("Clear Log")
|
139 |
+
with gr.Row():
|
140 |
+
analyze_button = gr.Button("Analyze")
|
141 |
+
alert_button = gr.Button("Generate Alert")
|
142 |
+
compare_button = gr.Button("Compare Last Two Logs")
|
143 |
+
output = gr.HTML(label="Analysis Summary")
|
144 |
+
alert_output = gr.HTML(label="Satellite Alert")
|
145 |
+
compare_output = gr.HTML(label="Log Comparison")
|
146 |
sample_button.click(fn=load_sample_log, outputs=log_input)
|
147 |
random_button.click(fn=generate_random_log, outputs=log_input)
|
148 |
clear_button.click(fn=clear_log, outputs=log_input)
|
149 |
+
analyze_button.click(fn=analyze_log, inputs=log_input, outputs=[output, output])
|
150 |
+
alert_button.click(fn=generate_alert, inputs=log_input, outputs=alert_output)
|
151 |
+
compare_button.click(fn=compare_logs, outputs=[compare_output, compare_output])
|
152 |
|
153 |
interface.launch()
|