Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ import pandas as pd
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import requests
|
| 8 |
import logging
|
|
|
|
| 9 |
from datetime import datetime, timedelta
|
| 10 |
from huggingface_hub import HfApi, hf_hub_download
|
| 11 |
import gradio as gr
|
|
@@ -93,6 +94,34 @@ def send_to_slack(user_url, hourly_url):
|
|
| 93 |
logging.error(f"Fel vid Slack-post: {e}", exc_info=True)
|
| 94 |
raise
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# --- Huvudfunktion ---
|
| 97 |
def run_report():
|
| 98 |
try:
|
|
@@ -103,12 +132,20 @@ def run_report():
|
|
| 103 |
repo_type="dataset",
|
| 104 |
token=HF_TOKEN
|
| 105 |
)
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
user_graph, hourly_graph = generate_graphs(df)
|
| 108 |
user_url, hourly_url = upload_graphs_to_hub(user_graph, hourly_graph)
|
| 109 |
send_to_slack(user_url, hourly_url)
|
| 110 |
logging.info("Rapport skickad till Slack.")
|
| 111 |
return "✅ Slack-meddelande skickat med grafer."
|
|
|
|
| 112 |
except Exception as e:
|
| 113 |
logging.error(f"Fel vid körning: {e}", exc_info=True)
|
| 114 |
return f"❌ Fel vid körning: {e}"
|
|
|
|
| 6 |
import matplotlib.pyplot as plt
|
| 7 |
import requests
|
| 8 |
import logging
|
| 9 |
+
import re
|
| 10 |
from datetime import datetime, timedelta
|
| 11 |
from huggingface_hub import HfApi, hf_hub_download
|
| 12 |
import gradio as gr
|
|
|
|
| 94 |
logging.error(f"Fel vid Slack-post: {e}", exc_info=True)
|
| 95 |
raise
|
| 96 |
|
| 97 |
+
# --- Konvertera gammal loggtext till DataFrame ---
|
| 98 |
+
def parse_legacy_log(log_path):
|
| 99 |
+
with open(log_path, "r", encoding="utf-8") as f:
|
| 100 |
+
lines = f.readlines()
|
| 101 |
+
|
| 102 |
+
sessions = []
|
| 103 |
+
current = {}
|
| 104 |
+
for line in lines:
|
| 105 |
+
ts_match = re.match(r"\[(.*?)\] (.*?)\: (.*)", line.strip())
|
| 106 |
+
if ts_match:
|
| 107 |
+
timestamp, role, content = ts_match.groups()
|
| 108 |
+
if role.lower() == "user":
|
| 109 |
+
current = {
|
| 110 |
+
"timestamp": timestamp,
|
| 111 |
+
"user_message": content,
|
| 112 |
+
"user_id": "unknown",
|
| 113 |
+
"session_id": "legacy"
|
| 114 |
+
}
|
| 115 |
+
elif role.lower() == "bot" and current:
|
| 116 |
+
current["bot_reply"] = content
|
| 117 |
+
sessions.append(current)
|
| 118 |
+
current = {}
|
| 119 |
+
df = pd.DataFrame(sessions)
|
| 120 |
+
df["timestamp"] = pd.to_datetime(df["timestamp"])
|
| 121 |
+
df["response_time"] = 1.0
|
| 122 |
+
df["platform"] = "legacy"
|
| 123 |
+
return df
|
| 124 |
+
|
| 125 |
# --- Huvudfunktion ---
|
| 126 |
def run_report():
|
| 127 |
try:
|
|
|
|
| 132 |
repo_type="dataset",
|
| 133 |
token=HF_TOKEN
|
| 134 |
)
|
| 135 |
+
|
| 136 |
+
try:
|
| 137 |
+
df = pd.read_json(log_path, lines=True)
|
| 138 |
+
logging.info("Loggfil läst som JSONL.")
|
| 139 |
+
except Exception as e:
|
| 140 |
+
logging.warning("Kunde inte läsa logg som JSONL – försöker konvertera från textformat.")
|
| 141 |
+
df = parse_legacy_log(log_path)
|
| 142 |
+
|
| 143 |
user_graph, hourly_graph = generate_graphs(df)
|
| 144 |
user_url, hourly_url = upload_graphs_to_hub(user_graph, hourly_graph)
|
| 145 |
send_to_slack(user_url, hourly_url)
|
| 146 |
logging.info("Rapport skickad till Slack.")
|
| 147 |
return "✅ Slack-meddelande skickat med grafer."
|
| 148 |
+
|
| 149 |
except Exception as e:
|
| 150 |
logging.error(f"Fel vid körning: {e}", exc_info=True)
|
| 151 |
return f"❌ Fel vid körning: {e}"
|