File size: 7,041 Bytes
a89d904
 
 
63afc1e
 
8dbfb64
a89d904
 
 
 
 
 
 
 
59b14ab
32fe223
 
 
59b14ab
 
56ac355
 
63afc1e
 
 
 
 
 
 
 
 
 
 
 
 
56ac355
 
63afc1e
 
 
 
 
a89d904
56ac355
8dbfb64
56ac355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dbfb64
 
 
 
 
56ac355
8dbfb64
56ac355
 
a89d904
 
 
 
 
 
 
56ac355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a89d904
 
8dbfb64
a89d904
 
 
 
56ac355
 
 
 
 
 
 
a89d904
56ac355
a89d904
59b14ab
 
 
2e45dd0
 
 
 
59b14ab
8dbfb64
59b14ab
63afc1e
 
 
2e45dd0
56ac355
 
 
 
 
8dbfb64
56ac355
 
59b14ab
63afc1e
2e45dd0
8dbfb64
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
import requests
import gradio as gr
import os
import random
from datetime import datetime, timedelta
import plotly.express as px

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:Arial;'><h3>Analysis Summary</h3><ul>"
        for line in summary.split("\n"):
            if "Issues:" in line:
                html += "<li><b style='color:red;'>Issues:</b><ul>"
            elif "High-priority messages:" in line:
                html += "</ul><li><b style='color:orange;'>High-priority messages:</b><ul>"
            elif "Key details:" in line:
                html += "</ul><li><b style='color:lightblue;'>Key details:</b><ul>"
            elif line.strip():
                html += f"<li>{line.strip()}</li>"
        html += "</ul></div>"
        # Extract signal strengths for plot
        signals = [int(s.split("%")[0]) for s in log_text.split("\n") if "Signal Strength" in s]
        times = [line.split(" | ")[0] for line in log_text.split("\n") if "Signal Strength" in s]
        fig = px.line(x=times, y=signals, labels={"x": "Time", "y": "Signal Strength (%)"}, title="Signal Strength Trend")
        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='background:red; 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": 300,  # Increased
        "temperature": 0.5
    }
    try:
        response = requests.post(url, json=data, headers=headers)
        comparison = response.json()["choices"][0]["message"]["content"]
        html = "<div style='font-family:Arial;'><h3>Log 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 ""

with gr.Blocks(theme="huggingface/dark") as interface:
    gr.Markdown("# Satellite Signal Log Analyzer")
    gr.Markdown("Analyze satellite radio logs for issues, alerts, trends, and signal visuals using Llama 4 and Cerebras.")
    log_input = gr.Textbox(lines=5, label="Satellite Radio Log")
    with gr.Row():
        sample_button = gr.Button("Load Sample Log")
        random_button = gr.Button("Generate Random Log")
        clear_button = gr.Button("Clear Log")
    with gr.Row():
        analyze_button = gr.Button("Analyze")
        alert_button = gr.Button("Generate Alert")
        compare_button = gr.Button("Compare Last Two Logs")
    output = gr.HTML(label="Analysis Summary")
    plot_output = gr.Plot(label="Signal Strength Trend")
    alert_output = gr.HTML(label="Satellite Alert")
    compare_output = gr.HTML(label="Log Comparison")
    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()