File size: 3,381 Bytes
7a49799
 
 
 
 
0626649
7a49799
 
 
 
 
 
 
5a5dffa
7a49799
0ccb0a5
5a5dffa
0ccb0a5
 
 
 
 
 
5a5dffa
0ccb0a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a49799
 
5a5dffa
7a49799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ccb0a5
 
 
 
 
 
 
7a49799
5a5dffa
 
 
 
7a49799
 
 
 
 
 
 
f40a5a2
7a49799
 
f40a5a2
7a49799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# 🔁 Replace with your actual Hugging Face model name
MODEL_NAME = "subhoshripal/smolified-context-bridge-slm"

# Load model + tokenizer
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)

# ---------- FORMAT OUTPUT ----------
def format_output(text):
    impact, business, action = "N/A", "N/A", "N/A"

    text = text.lower()

    # Try to extract sections flexibly
    if "impact" in text:
        try:
            impact = text.split("impact")[1].split("\n")[0].replace("level", "").replace(":", "").strip().title()
        except:
            pass

    if "business" in text:
        try:
            business = text.split("business")[1].split("strategic")[0].replace("translation", "").replace(":", "").strip().capitalize()
        except:
            pass

    if "strategic" in text or "action" in text:
        try:
            action = text.split("strategic")[-1].replace("action", "").replace(":", "").strip().capitalize()
        except:
            pass

    # FALLBACK: if parsing failed → use whole text smartly
    if business == "N/A":
        business = text[:120].capitalize()

    if action == "N/A":
        action = "Investigate the issue and apply appropriate fixes."

    if impact == "N/A":
        impact = "Moderate"

    return impact, business, action
    
# ---------- MODEL FUNCTION ----------
def analyze(text):
    if not text.strip():
        return "N/A", "Please enter a system log.", "N/A"

    prompt = f"""
You are an Industrial Systems Analyst.

Analyze the following technical log and respond STRICTLY in this format:

Impact Level (Low/Moderate/High/Critical):
Business Translation (1-2 lines, clear and concise):
Strategic Action (specific next step):

Log:
{text}
"""

    inputs = tokenizer(prompt, return_tensors="pt")

    with torch.no_grad():
        outputs = model.generate(
    **inputs,
    max_new_tokens=100,
    temperature=0.2,   # more stable
    top_p=0.9,
    do_sample=True,
    repetition_penalty=1.3
)

    result = tokenizer.decode(
    outputs[0][inputs["input_ids"].shape[-1]:],
    skip_special_tokens=True
    )
    impact, business, action = format_output(result)

    return impact, business, action

# ---------- UI ----------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # ContextBridge: From Logs to Decisions  
    ### Translate technical errors into business insights in seconds
    
    Paste a system log below
    """)

    input_box = gr.Textbox(
        placeholder="e.g. API timeout after 3000ms while fetching user data",
        label="System Log",
        lines=3
    )

    gr.Examples(
        examples=[
            "API timeout after 3000ms while fetching user data",
            "Unauthorized access attempt from IP 192.168.1.5",
            "Database connection refused after multiple retries"
        ],
        inputs=input_box
    )

    analyze_btn = gr.Button("Analyze ⚡")

    impact_output = gr.Textbox(label="Impact Level")
    business_output = gr.Textbox(label="Business Translation")
    action_output = gr.Textbox(label="Strategic Action")

    analyze_btn.click(
        analyze,
        inputs=input_box,
        outputs=[impact_output, business_output, action_output]
    )

demo.launch()