Spaces:
Sleeping
Sleeping
Commit
Β·
cab03d6
1
Parent(s):
1eb66c4
Updated
Browse files
main.py
CHANGED
|
@@ -5,8 +5,9 @@ from fastapi.middleware.cors import CORSMiddleware
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from spitch import Spitch
|
| 7 |
from langchain.prompts import PromptTemplate
|
| 8 |
-
from langchain_huggingface import HuggingFaceEndpoint
|
| 9 |
from langdetect import detect, DetectorFactory
|
|
|
|
| 10 |
|
| 11 |
DetectorFactory.seed = 0
|
| 12 |
|
|
@@ -23,7 +24,7 @@ if not SPITCH_API_KEY:
|
|
| 23 |
os.environ["SPITCH_API_KEY"] = SPITCH_API_KEY
|
| 24 |
spitch_client = Spitch()
|
| 25 |
|
| 26 |
-
# β
|
| 27 |
llm = HuggingFaceEndpoint(
|
| 28 |
repo_id=HF_MODEL,
|
| 29 |
temperature=0.2,
|
|
@@ -57,7 +58,6 @@ Question: {question}
|
|
| 57 |
Answer:
|
| 58 |
"""
|
| 59 |
|
| 60 |
-
|
| 61 |
stt_chat_template = """You are DevAssist, an AI coding assistant.
|
| 62 |
- The input is transcribed speech. Interpret it as a dev question.
|
| 63 |
- Provide clear answers with code examples (use markdown triple backticks).
|
|
@@ -126,8 +126,15 @@ def root():
|
|
| 126 |
@app.post("/chat")
|
| 127 |
def chat(req: ChatRequest, authorization: str | None = Header(None)):
|
| 128 |
check_auth(authorization)
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
@app.post("/stt")
|
| 133 |
async def stt_audio(file: UploadFile = File(...), lang_hint: str | None = None, authorization: str | None = Header(None)):
|
|
@@ -189,14 +196,21 @@ async def sme_generate(payload: dict = Body(...)):
|
|
| 189 |
"language": "en"
|
| 190 |
}
|
| 191 |
"""
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
# β
Hugging Face requires port 7860, not 8000
|
| 202 |
if __name__ == "__main__":
|
|
|
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from spitch import Spitch
|
| 7 |
from langchain.prompts import PromptTemplate
|
| 8 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 9 |
from langdetect import detect, DetectorFactory
|
| 10 |
+
from huggingface_hub.utils import HfHubHTTPError # β
for quota error handling
|
| 11 |
|
| 12 |
DetectorFactory.seed = 0
|
| 13 |
|
|
|
|
| 24 |
os.environ["SPITCH_API_KEY"] = SPITCH_API_KEY
|
| 25 |
spitch_client = Spitch()
|
| 26 |
|
| 27 |
+
# β
HuggingFaceEndpoint
|
| 28 |
llm = HuggingFaceEndpoint(
|
| 29 |
repo_id=HF_MODEL,
|
| 30 |
temperature=0.2,
|
|
|
|
| 58 |
Answer:
|
| 59 |
"""
|
| 60 |
|
|
|
|
| 61 |
stt_chat_template = """You are DevAssist, an AI coding assistant.
|
| 62 |
- The input is transcribed speech. Interpret it as a dev question.
|
| 63 |
- Provide clear answers with code examples (use markdown triple backticks).
|
|
|
|
| 126 |
@app.post("/chat")
|
| 127 |
def chat(req: ChatRequest, authorization: str | None = Header(None)):
|
| 128 |
check_auth(authorization)
|
| 129 |
+
try:
|
| 130 |
+
answer = chat_chain.invoke({"question": req.question})
|
| 131 |
+
return {"reply": answer.strip() if isinstance(answer, str) else str(answer)}
|
| 132 |
+
except HfHubHTTPError as e:
|
| 133 |
+
if "exceeded" in str(e).lower() or "quota" in str(e).lower():
|
| 134 |
+
return {
|
| 135 |
+
"reply": "β οΈ Token quota for today has been used. Please come back in 24 hours."
|
| 136 |
+
}
|
| 137 |
+
raise e
|
| 138 |
|
| 139 |
@app.post("/stt")
|
| 140 |
async def stt_audio(file: UploadFile = File(...), lang_hint: str | None = None, authorization: str | None = Header(None)):
|
|
|
|
| 196 |
"language": "en"
|
| 197 |
}
|
| 198 |
"""
|
| 199 |
+
try:
|
| 200 |
+
response = sme_chain.invoke({
|
| 201 |
+
"site_name": payload.get("site_name"),
|
| 202 |
+
"stack": payload.get("stack"),
|
| 203 |
+
"pages": payload.get("pages"),
|
| 204 |
+
"language": payload.get("language"),
|
| 205 |
+
})
|
| 206 |
+
return {"success": True, "data": response}
|
| 207 |
+
except HfHubHTTPError as e:
|
| 208 |
+
if "exceeded" in str(e).lower() or "quota" in str(e).lower():
|
| 209 |
+
return {
|
| 210 |
+
"success": False,
|
| 211 |
+
"error": "β οΈ Token quota for today has been used. Please come back in 24 hours."
|
| 212 |
+
}
|
| 213 |
+
raise e
|
| 214 |
|
| 215 |
# β
Hugging Face requires port 7860, not 8000
|
| 216 |
if __name__ == "__main__":
|