Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -25,7 +25,7 @@ if not SPITCH_API_KEY:
|
|
| 25 |
os.environ["SPITCH_API_KEY"] = SPITCH_API_KEY
|
| 26 |
spitch_client = Spitch()
|
| 27 |
|
| 28 |
-
# HuggingFace LLM
|
| 29 |
llm = HuggingFaceEndpoint(
|
| 30 |
repo_id=HF_MODEL,
|
| 31 |
temperature=0.7,
|
|
@@ -78,7 +78,6 @@ Code: {code}
|
|
| 78 |
Documentation:
|
| 79 |
"""
|
| 80 |
|
| 81 |
-
# 🔥 Fixed SME template with escaped curly braces
|
| 82 |
sme_template = """
|
| 83 |
You are a senior full-stack engineer specializing in modern front-end development.
|
| 84 |
Your job is to generate **production-ready code** for websites and apps.
|
|
@@ -90,7 +89,6 @@ Guidelines:
|
|
| 90 |
- JavaScript must add interactivity (animations, toggles, button actions)
|
| 91 |
- Include hero, feature grid, testimonials, and footer
|
| 92 |
- Use realistic content (no lorem ipsum, no placeholders)
|
| 93 |
-
- Return ONLY valid JSON: {{ "files": {{ "index.html": "...", "styles.css": "...", "script.js": "..." }} }}
|
| 94 |
|
| 95 |
Prompt: {user_prompt}
|
| 96 |
Context: {context}
|
|
@@ -114,7 +112,7 @@ class AutoDocRequest(BaseModel):
|
|
| 114 |
# ----------------- AUTH -----------------
|
| 115 |
def check_auth(authorization: str | None):
|
| 116 |
"""Validate Bearer token against PROJECT_API_KEY"""
|
| 117 |
-
if not PROJECT_API_KEY:
|
| 118 |
return
|
| 119 |
if not authorization or not authorization.startswith("Bearer "):
|
| 120 |
raise HTTPException(status_code=401, detail="Missing bearer token")
|
|
@@ -193,10 +191,54 @@ async def sme_generate(payload: dict = Body(...), authorization: str | None = He
|
|
| 193 |
context = "\n".join([doc.page_content for doc in context_docs]) if context_docs else "No extra context"
|
| 194 |
response = sme_chain.invoke({"user_prompt": user_prompt, "context": context})
|
| 195 |
return {"success": True, "data": response}
|
| 196 |
-
except
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
# ----------------- MAIN -----------------
|
| 202 |
if __name__ == "__main__":
|
|
|
|
| 25 |
os.environ["SPITCH_API_KEY"] = SPITCH_API_KEY
|
| 26 |
spitch_client = Spitch()
|
| 27 |
|
| 28 |
+
# HuggingFace LLM (removed task="conversational" to avoid StopIteration bug)
|
| 29 |
llm = HuggingFaceEndpoint(
|
| 30 |
repo_id=HF_MODEL,
|
| 31 |
temperature=0.7,
|
|
|
|
| 78 |
Documentation:
|
| 79 |
"""
|
| 80 |
|
|
|
|
| 81 |
sme_template = """
|
| 82 |
You are a senior full-stack engineer specializing in modern front-end development.
|
| 83 |
Your job is to generate **production-ready code** for websites and apps.
|
|
|
|
| 89 |
- JavaScript must add interactivity (animations, toggles, button actions)
|
| 90 |
- Include hero, feature grid, testimonials, and footer
|
| 91 |
- Use realistic content (no lorem ipsum, no placeholders)
|
|
|
|
| 92 |
|
| 93 |
Prompt: {user_prompt}
|
| 94 |
Context: {context}
|
|
|
|
| 112 |
# ----------------- AUTH -----------------
|
| 113 |
def check_auth(authorization: str | None):
|
| 114 |
"""Validate Bearer token against PROJECT_API_KEY"""
|
| 115 |
+
if not PROJECT_API_KEY: # If not set, skip auth
|
| 116 |
return
|
| 117 |
if not authorization or not authorization.startswith("Bearer "):
|
| 118 |
raise HTTPException(status_code=401, detail="Missing bearer token")
|
|
|
|
| 191 |
context = "\n".join([doc.page_content for doc in context_docs]) if context_docs else "No extra context"
|
| 192 |
response = sme_chain.invoke({"user_prompt": user_prompt, "context": context})
|
| 193 |
return {"success": True, "data": response}
|
| 194 |
+
except Exception as e:
|
| 195 |
+
return {"success": False, "error": f"⚠️ LLM error: {str(e)}"}
|
| 196 |
+
|
| 197 |
+
@app.post("/sme/speech-generate")
|
| 198 |
+
async def sme_speech_generate(file: UploadFile = File(...), lang_hint: str | None = None, authorization: str | None = Header(None)):
|
| 199 |
+
check_auth(authorization)
|
| 200 |
+
|
| 201 |
+
suffix = os.path.splitext(file.filename)[1] or ".wav"
|
| 202 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tf:
|
| 203 |
+
tf.write(await file.read())
|
| 204 |
+
tmp_path = tf.name
|
| 205 |
+
|
| 206 |
+
try:
|
| 207 |
+
if lang_hint:
|
| 208 |
+
resp = spitch_client.speech.transcribe(language=lang_hint, content=open(tmp_path, "rb").read())
|
| 209 |
+
else:
|
| 210 |
+
resp = spitch_client.speech.transcribe(content=open(tmp_path, "rb").read())
|
| 211 |
+
except Exception:
|
| 212 |
+
resp = spitch_client.speech.transcribe(language="en", content=open(tmp_path, "rb").read())
|
| 213 |
+
|
| 214 |
+
transcription = getattr(resp, "text", "") or (resp.get("text", "") if isinstance(resp, dict) else "")
|
| 215 |
+
detected_lang = "en"
|
| 216 |
+
try:
|
| 217 |
+
detected_lang = detect(transcription) if transcription.strip() else "en"
|
| 218 |
+
except Exception:
|
| 219 |
+
pass
|
| 220 |
+
|
| 221 |
+
translation = transcription
|
| 222 |
+
if detected_lang != "en":
|
| 223 |
+
try:
|
| 224 |
+
translation_resp = spitch_client.text.translate(text=transcription, source=detected_lang, target="en")
|
| 225 |
+
translation = getattr(translation_resp, "text", "") or translation_resp.get("text", "")
|
| 226 |
+
except Exception:
|
| 227 |
+
translation = transcription
|
| 228 |
+
|
| 229 |
+
try:
|
| 230 |
+
context_docs = retriever.get_relevant_documents(translation)
|
| 231 |
+
context = "\n".join([doc.page_content for doc in context_docs]) if context_docs else "No extra context"
|
| 232 |
+
sme_response = sme_chain.invoke({"user_prompt": translation, "context": context})
|
| 233 |
+
return {
|
| 234 |
+
"success": True,
|
| 235 |
+
"transcription": transcription,
|
| 236 |
+
"detected_language": detected_lang,
|
| 237 |
+
"translation": translation,
|
| 238 |
+
"sme_site": sme_response
|
| 239 |
+
}
|
| 240 |
+
except Exception as e:
|
| 241 |
+
return {"success": False, "error": f"⚠️ LLM error: {str(e)}"}
|
| 242 |
|
| 243 |
# ----------------- MAIN -----------------
|
| 244 |
if __name__ == "__main__":
|