Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import random
|
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
import plotly.express as px
|
| 7 |
import re
|
|
|
|
| 8 |
|
| 9 |
api_key = os.getenv("CEREBRAS_API_KEY")
|
| 10 |
url = "https://api.cerebras.ai/v1/chat/completions"
|
|
@@ -41,16 +42,31 @@ def generate_random_log():
|
|
| 41 |
entries.append(f"{time} | {lat}N, {lon}W | Frequency: {freq} GHz | Signal Strength: {signal}% | Message: {message}")
|
| 42 |
return "\n".join(entries)
|
| 43 |
|
| 44 |
-
def
|
| 45 |
if not log_text.strip():
|
| 46 |
-
return
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
data = {
|
| 49 |
"model": "llama-4-scout-17b-16e-instruct",
|
| 50 |
"messages": [
|
| 51 |
{
|
| 52 |
"role": "user",
|
| 53 |
-
"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" +
|
| 54 |
}
|
| 55 |
],
|
| 56 |
"max_completion_tokens": 500,
|
|
@@ -72,7 +88,7 @@ def analyze_log(log_text):
|
|
| 72 |
html += "</ul></div>"
|
| 73 |
signals = []
|
| 74 |
times = []
|
| 75 |
-
for line in
|
| 76 |
if "Signal Strength" in line:
|
| 77 |
match = re.search(r"Signal Strength: (\d+)%", line)
|
| 78 |
if match:
|
|
@@ -81,9 +97,10 @@ def analyze_log(log_text):
|
|
| 81 |
if time_match:
|
| 82 |
times.append(time_match.group(1)[-5:])
|
| 83 |
fig = px.line(x=times, y=signals, labels={"x": "Time", "y": "Signal (%)"}, title="Signal Trend") if signals and len(signals) == len(times) else None
|
| 84 |
-
|
|
|
|
| 85 |
except Exception as e:
|
| 86 |
-
return f"Error: API call failed - {str(e)}", None, None
|
| 87 |
|
| 88 |
def generate_alert(log_text):
|
| 89 |
if not log_text.strip():
|
|
@@ -151,6 +168,7 @@ with gr.Blocks(css=css) as interface:
|
|
| 151 |
gr.Markdown("# Satellite Signal Log Analyzer", elem_classes="header")
|
| 152 |
gr.Markdown("Analyze logs for issues, alerts, and trends.", elem_classes="subheader")
|
| 153 |
log_input = gr.Textbox(lines=5, show_label=False, placeholder="Enter or generate a log...")
|
|
|
|
| 154 |
with gr.Row():
|
| 155 |
sample_button = gr.Button("Sample Log")
|
| 156 |
random_button = gr.Button("Random Log")
|
|
@@ -159,15 +177,18 @@ with gr.Blocks(css=css) as interface:
|
|
| 159 |
analyze_button = gr.Button("Analyze")
|
| 160 |
alert_button = gr.Button("Alert")
|
| 161 |
compare_button = gr.Button("Compare Logs")
|
|
|
|
| 162 |
output = gr.HTML(show_label=False)
|
| 163 |
plot_output = gr.Plot(show_label=False)
|
| 164 |
alert_output = gr.HTML(show_label=False)
|
| 165 |
compare_output = gr.HTML(show_label=False)
|
|
|
|
| 166 |
sample_button.click(fn=load_sample_log, outputs=log_input)
|
| 167 |
random_button.click(fn=generate_random_log, outputs=log_input)
|
| 168 |
clear_button.click(fn=clear_log, outputs=log_input)
|
| 169 |
-
analyze_button.click(fn=analyze_log, inputs=log_input, outputs=[output, output, plot_output])
|
| 170 |
alert_button.click(fn=generate_alert, inputs=log_input, outputs=alert_output)
|
| 171 |
compare_button.click(fn=compare_logs, outputs=[compare_output, compare_output])
|
|
|
|
| 172 |
|
| 173 |
interface.launch()
|
|
|
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
import plotly.express as px
|
| 7 |
import re
|
| 8 |
+
import io
|
| 9 |
|
| 10 |
api_key = os.getenv("CEREBRAS_API_KEY")
|
| 11 |
url = "https://api.cerebras.ai/v1/chat/completions"
|
|
|
|
| 42 |
entries.append(f"{time} | {lat}N, {lon}W | Frequency: {freq} GHz | Signal Strength: {signal}% | Message: {message}")
|
| 43 |
return "\n".join(entries)
|
| 44 |
|
| 45 |
+
def filter_log_by_frequency(log_text, min_freq, max_freq):
|
| 46 |
if not log_text.strip():
|
| 47 |
+
return log_text
|
| 48 |
+
filtered = []
|
| 49 |
+
for line in log_text.split("\n"):
|
| 50 |
+
match = re.search(r"Frequency: (\d+\.\d) GHz", line)
|
| 51 |
+
if match:
|
| 52 |
+
freq = float(match.group(1))
|
| 53 |
+
if min_freq <= freq <= max_freq:
|
| 54 |
+
filtered.append(line)
|
| 55 |
+
else:
|
| 56 |
+
filtered.append(line) # Keep non-frequency lines
|
| 57 |
+
return "\n".join(filtered) if filtered else "No logs in frequency range."
|
| 58 |
+
|
| 59 |
+
def analyze_log(log_text, min_freq, max_freq):
|
| 60 |
+
filtered_log = filter_log_by_frequency(log_text, min_freq, max_freq)
|
| 61 |
+
if not filtered_log.strip() or filtered_log.startswith("No logs"):
|
| 62 |
+
return filtered_log, None, None, None
|
| 63 |
+
LOG_HISTORY.append(filtered_log)
|
| 64 |
data = {
|
| 65 |
"model": "llama-4-scout-17b-16e-instruct",
|
| 66 |
"messages": [
|
| 67 |
{
|
| 68 |
"role": "user",
|
| 69 |
+
"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" + filtered_log
|
| 70 |
}
|
| 71 |
],
|
| 72 |
"max_completion_tokens": 500,
|
|
|
|
| 88 |
html += "</ul></div>"
|
| 89 |
signals = []
|
| 90 |
times = []
|
| 91 |
+
for line in filtered_log.split("\n"):
|
| 92 |
if "Signal Strength" in line:
|
| 93 |
match = re.search(r"Signal Strength: (\d+)%", line)
|
| 94 |
if match:
|
|
|
|
| 97 |
if time_match:
|
| 98 |
times.append(time_match.group(1)[-5:])
|
| 99 |
fig = px.line(x=times, y=signals, labels={"x": "Time", "y": "Signal (%)"}, title="Signal Trend") if signals and len(signals) == len(times) else None
|
| 100 |
+
export_file = io.StringIO(summary)
|
| 101 |
+
return summary, html, fig, gr.File(value=export_file, file_name="summary.txt", visible=True)
|
| 102 |
except Exception as e:
|
| 103 |
+
return f"Error: API call failed - {str(e)}", None, None, None
|
| 104 |
|
| 105 |
def generate_alert(log_text):
|
| 106 |
if not log_text.strip():
|
|
|
|
| 168 |
gr.Markdown("# Satellite Signal Log Analyzer", elem_classes="header")
|
| 169 |
gr.Markdown("Analyze logs for issues, alerts, and trends.", elem_classes="subheader")
|
| 170 |
log_input = gr.Textbox(lines=5, show_label=False, placeholder="Enter or generate a log...")
|
| 171 |
+
freq_slider = gr.Slider(minimum=14.0, maximum=15.0, step=0.1, value=[14.0, 15.0], label="Frequency Range (GHz)")
|
| 172 |
with gr.Row():
|
| 173 |
sample_button = gr.Button("Sample Log")
|
| 174 |
random_button = gr.Button("Random Log")
|
|
|
|
| 177 |
analyze_button = gr.Button("Analyze")
|
| 178 |
alert_button = gr.Button("Alert")
|
| 179 |
compare_button = gr.Button("Compare Logs")
|
| 180 |
+
export_button = gr.Button("Export")
|
| 181 |
output = gr.HTML(show_label=False)
|
| 182 |
plot_output = gr.Plot(show_label=False)
|
| 183 |
alert_output = gr.HTML(show_label=False)
|
| 184 |
compare_output = gr.HTML(show_label=False)
|
| 185 |
+
export_output = gr.File(show_label=False, visible=False)
|
| 186 |
sample_button.click(fn=load_sample_log, outputs=log_input)
|
| 187 |
random_button.click(fn=generate_random_log, outputs=log_input)
|
| 188 |
clear_button.click(fn=clear_log, outputs=log_input)
|
| 189 |
+
analyze_button.click(fn=analyze_log, inputs=[log_input, freq_slider], outputs=[output, output, plot_output, export_output])
|
| 190 |
alert_button.click(fn=generate_alert, inputs=log_input, outputs=alert_output)
|
| 191 |
compare_button.click(fn=compare_logs, outputs=[compare_output, compare_output])
|
| 192 |
+
export_button.click(fn=lambda: None, outputs=export_output)
|
| 193 |
|
| 194 |
interface.launch()
|