Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
-
from fastapi import FastAPI
|
| 5 |
-
from pydantic import BaseModel
|
| 6 |
|
| 7 |
-
# 🔐 Token
|
| 8 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
| 9 |
|
| 10 |
-
# ✅ Use router endpoint (good choice)
|
| 11 |
-
API_URL = "https://api-inference.huggingface.co/models/dali4444444/chery-sav-chatbot"
|
| 12 |
-
# 🧠 System prompt
|
| 13 |
SYSTEM_PROMPT = """Tu es l'assistant SAV officiel du Centre Chery Tunisie.
|
| 14 |
Tu réponds en français ou en arabe dialectal tunisien selon la langue du client.
|
| 15 |
-
Réponds uniquement aux questions liées aux véhicules Chery
|
| 16 |
|
| 17 |
-
# 💬 Chat function
|
| 18 |
def chat(message, history):
|
| 19 |
prompt = f"[INST] <<SYS>>\n{SYSTEM_PROMPT}\n<</SYS>>\n\n"
|
| 20 |
|
| 21 |
for h in history:
|
| 22 |
-
|
| 23 |
-
if h.get("role") == "user":
|
| 24 |
-
prompt += f"{h.get('content')} [/INST] "
|
| 25 |
-
else:
|
| 26 |
-
prompt += f"{h.get('content')} </s><s>[INST] "
|
| 27 |
-
else:
|
| 28 |
-
prompt += f"{h[0]} [/INST] {h[1]} </s><s>[INST] "
|
| 29 |
|
| 30 |
prompt += f"{message} [/INST]"
|
| 31 |
|
| 32 |
-
headers = {
|
| 33 |
-
"Authorization": f"Bearer {HF_TOKEN}",
|
| 34 |
-
"Content-Type": "application/json"
|
| 35 |
-
}
|
| 36 |
|
| 37 |
payload = {
|
| 38 |
"inputs": prompt,
|
|
@@ -47,50 +32,21 @@ def chat(message, history):
|
|
| 47 |
|
| 48 |
try:
|
| 49 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
| 50 |
-
|
| 51 |
-
# ✅ Check HTTP error
|
| 52 |
-
if response.status_code != 200:
|
| 53 |
-
return f"Erreur HTTP {response.status_code}: {response.text}"
|
| 54 |
-
|
| 55 |
result = response.json()
|
| 56 |
|
| 57 |
-
# ✅ Handle response formats
|
| 58 |
if isinstance(result, list):
|
| 59 |
return result[0].get("generated_text", "Pas de réponse")
|
| 60 |
|
| 61 |
-
if
|
| 62 |
-
|
| 63 |
-
if "loading" in result["error"].lower():
|
| 64 |
-
return "⏳ Le modèle est en cours de chargement, réessayez dans quelques secondes."
|
| 65 |
-
return f"Erreur API: {result['error']}"
|
| 66 |
|
| 67 |
return str(result)
|
| 68 |
|
| 69 |
-
except requests.exceptions.Timeout:
|
| 70 |
-
return "⏱️ Timeout: le modèle met trop de temps à répondre."
|
| 71 |
except Exception as e:
|
| 72 |
return f"Erreur: {str(e)}"
|
| 73 |
|
| 74 |
|
| 75 |
-
|
| 76 |
-
app = FastAPI()
|
| 77 |
-
|
| 78 |
-
class ChatRequest(BaseModel):
|
| 79 |
-
message: str
|
| 80 |
-
history: list = []
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
@app.post("/api/chat")
|
| 84 |
-
async def api_chat(req: ChatRequest):
|
| 85 |
-
return {"reply": chat(req.message, req.history)}
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# 🎨 Gradio UI
|
| 89 |
-
demo = gr.ChatInterface(
|
| 90 |
-
fn=chat,
|
| 91 |
-
title="🚗 Chery SAV Assistant",
|
| 92 |
-
description="Assistant intelligent pour le service après-vente Chery Tunisie"
|
| 93 |
-
)
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
|
|
|
| 5 |
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 6 |
+
API_URL = "https://router.huggingface.co/hf-inference/models/mistralai/Mistral-7B-Instruct-v0.3"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
SYSTEM_PROMPT = """Tu es l'assistant SAV officiel du Centre Chery Tunisie.
|
| 9 |
Tu réponds en français ou en arabe dialectal tunisien selon la langue du client.
|
| 10 |
+
Réponds uniquement aux questions liées aux véhicules Chery."""
|
| 11 |
|
|
|
|
| 12 |
def chat(message, history):
|
| 13 |
prompt = f"[INST] <<SYS>>\n{SYSTEM_PROMPT}\n<</SYS>>\n\n"
|
| 14 |
|
| 15 |
for h in history:
|
| 16 |
+
prompt += f"{h[0]} [/INST] {h[1]} </s><s>[INST] "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
prompt += f"{message} [/INST]"
|
| 19 |
|
| 20 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
payload = {
|
| 23 |
"inputs": prompt,
|
|
|
|
| 32 |
|
| 33 |
try:
|
| 34 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
result = response.json()
|
| 36 |
|
|
|
|
| 37 |
if isinstance(result, list):
|
| 38 |
return result[0].get("generated_text", "Pas de réponse")
|
| 39 |
|
| 40 |
+
if "error" in result:
|
| 41 |
+
return f"Erreur: {result['error']}"
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
return str(result)
|
| 44 |
|
|
|
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Erreur: {str(e)}"
|
| 47 |
|
| 48 |
|
| 49 |
+
demo = gr.ChatInterface(fn=chat, title="🚗 Chery SAV Assistant")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# 🔥 THIS LINE IS THE KEY
|
| 52 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|