Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +41 -20
src/streamlit_app.py
CHANGED
|
@@ -2,12 +2,12 @@ import os
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import streamlit as st
|
| 5 |
-
import plotly.express as px
|
| 6 |
-
import plotly.figure_factory as ff
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from huggingface_hub import InferenceClient, login
|
| 9 |
import google.generativeai as genai
|
| 10 |
from io import StringIO
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# ======================================================
|
| 13 |
# ⚙️ APP CONFIGURATION
|
|
@@ -70,25 +70,36 @@ if ANALYST_MODEL != "Gemini 2.5 Flash (Google)":
|
|
| 70 |
# ======================================================
|
| 71 |
# 🧩 SAFE GENERATION FUNCTION
|
| 72 |
# ======================================================
|
| 73 |
-
def safe_hf_generate(client, prompt, temperature=0.3, max_tokens=512):
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
return_full_text=False,
|
| 80 |
-
)
|
| 81 |
-
return resp.strip()
|
| 82 |
-
except Exception as e:
|
| 83 |
-
if "Supported task: conversational" in str(e):
|
| 84 |
-
chat_resp = client.chat_completion(
|
| 85 |
-
messages=[{"role": "user", "content": prompt}],
|
| 86 |
-
max_tokens=max_tokens,
|
| 87 |
temperature=temperature,
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
-
return
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
# ======================================================
|
| 94 |
# 🧩 DATA CLEANING
|
|
@@ -183,8 +194,18 @@ Respond with:
|
|
| 183 |
)
|
| 184 |
return response.text if hasattr(response, "text") else "No valid text response."
|
| 185 |
else:
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
except Exception as e:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
return f"⚠️ Analysis failed: {str(e)}"
|
| 189 |
|
| 190 |
# ======================================================
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
from huggingface_hub import InferenceClient, login
|
| 7 |
import google.generativeai as genai
|
| 8 |
from io import StringIO
|
| 9 |
+
import time
|
| 10 |
+
import requests
|
| 11 |
|
| 12 |
# ======================================================
|
| 13 |
# ⚙️ APP CONFIGURATION
|
|
|
|
| 70 |
# ======================================================
|
| 71 |
# 🧩 SAFE GENERATION FUNCTION
|
| 72 |
# ======================================================
|
| 73 |
+
def safe_hf_generate(client, prompt, temperature=0.3, max_tokens=512, retries=2):
|
| 74 |
+
"""Try text generation, with retry + fallback on service errors."""
|
| 75 |
+
for attempt in range(retries + 1):
|
| 76 |
+
try:
|
| 77 |
+
resp = client.text_generation(
|
| 78 |
+
prompt,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
temperature=temperature,
|
| 80 |
+
max_new_tokens=max_tokens,
|
| 81 |
+
return_full_text=False,
|
| 82 |
)
|
| 83 |
+
return resp.strip()
|
| 84 |
+
except Exception as e:
|
| 85 |
+
err = str(e)
|
| 86 |
+
# 🩹 FIX: Handle common server overloads gracefully
|
| 87 |
+
if "503" in err or "Service Temporarily Unavailable" in err:
|
| 88 |
+
time.sleep(2)
|
| 89 |
+
if attempt < retries:
|
| 90 |
+
continue # retry
|
| 91 |
+
else:
|
| 92 |
+
return "⚠️ The Hugging Face model is temporarily unavailable. Please try again or switch to Gemini."
|
| 93 |
+
elif "Supported task: conversational" in err:
|
| 94 |
+
chat_resp = client.chat_completion(
|
| 95 |
+
messages=[{"role": "user", "content": prompt}],
|
| 96 |
+
max_tokens=max_tokens,
|
| 97 |
+
temperature=temperature,
|
| 98 |
+
)
|
| 99 |
+
return chat_resp["choices"][0]["message"]["content"].strip()
|
| 100 |
+
else:
|
| 101 |
+
raise e
|
| 102 |
+
return "⚠️ Failed after retries."
|
| 103 |
|
| 104 |
# ======================================================
|
| 105 |
# 🧩 DATA CLEANING
|
|
|
|
| 194 |
)
|
| 195 |
return response.text if hasattr(response, "text") else "No valid text response."
|
| 196 |
else:
|
| 197 |
+
# 🩹 FIX: wrap in retry-aware generator
|
| 198 |
+
result = safe_hf_generate(hf_analyst_client, prompt, temperature=temperature, max_tokens=max_tokens)
|
| 199 |
+
# fallback to Gemini if Hugging Face failed entirely
|
| 200 |
+
if "temporarily unavailable" in result.lower() and GEMINI_API_KEY:
|
| 201 |
+
alt = genai.GenerativeModel("gemini-2.5-flash").generate_content(prompt)
|
| 202 |
+
return f"🔄 Fallback to Gemini:\n\n{alt.text}"
|
| 203 |
+
return result
|
| 204 |
except Exception as e:
|
| 205 |
+
# 🩹 FIX: fallback if server rejects or 5xx
|
| 206 |
+
if "503" in str(e) and GEMINI_API_KEY:
|
| 207 |
+
response = genai.GenerativeModel("gemini-2.5-flash").generate_content(prompt)
|
| 208 |
+
return f"🔄 Fallback to Gemini due to 503 error:\n\n{response.text}"
|
| 209 |
return f"⚠️ Analysis failed: {str(e)}"
|
| 210 |
|
| 211 |
# ======================================================
|