ekhk01 commited on
Commit
11562c4
·
verified ·
1 Parent(s): 16c7cdd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+ import pandas as pd
5
+
6
+ # Hugging Face Inference API endpoint for DeepSeek
7
+ API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-V2"
8
+ headers = {"Authorization": f"Bearer {os.environ['HF_API_TOKEN']}"}
9
+
10
+ def preprocess_csv(file_path):
11
+ df = pd.read_csv(file_path)
12
+ events = []
13
+ for _, row in df.iterrows():
14
+ events.append(f"On {row['Date']} at {row['Time']}, state was {row['State']} → {row['Message Text']}")
15
+ return "\n".join(events)
16
+
17
+ def analyze_log(file_path, mode):
18
+ text = preprocess_csv(file_path)
19
+
20
+ # Build prompt depending on mode
21
+ if mode == "Summarize":
22
+ prompt = "Summarize the following AHU alarm log:\n\n" + text
23
+ elif mode == "Highlight anomalies":
24
+ prompt = "Identify unusual or repeated alarms in this AHU log and explain possible causes:\n\n" + text
25
+ elif mode == "Suggest maintenance":
26
+ prompt = "Based on this AHU alarm log, suggest maintenance actions:\n\n" + text
27
+ else:
28
+ prompt = text
29
+
30
+ # Call Hugging Face Inference API
31
+ response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
32
+
33
+ if response.status_code != 200:
34
+ return f"Error {response.status_code}: {response.text}"
35
+
36
+ try:
37
+ result = response.json()
38
+ except Exception:
39
+ return f"Failed to decode JSON. Raw response: {response.text}"
40
+
41
+ # Extract generated text
42
+ if isinstance(result, list) and "generated_text" in result[0]:
43
+ return result[0]["generated_text"]
44
+ elif isinstance(result, dict) and "generated_text" in result:
45
+ return result["generated_text"]
46
+ else:
47
+ return str(result)
48
+
49
+ iface = gr.Interface(
50
+ fn=analyze_log,
51
+ inputs=[
52
+ gr.File(type="filepath", label="Upload Log File"),
53
+ gr.Dropdown(choices=["Summarize", "Highlight anomalies", "Suggest maintenance"], label="Analysis Mode")
54
+ ],
55
+ outputs="text",
56
+ title="AHU Log Analyzer (DeepSeek API)",
57
+ description="Upload your log file (CSV) and choose how you want it analyzed using DeepSeek via Hugging Face API."
58
+ )
59
+
60
+ if __name__ == "__main__":
61
+ iface.launch()