dali4444444 commited on
Commit
9f63bc5
·
verified ·
1 Parent(s): 7627fe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -25
app.py CHANGED
@@ -2,21 +2,10 @@ import gradio as gr
2
  import os
3
  from huggingface_hub import hf_hub_download
4
  from llama_cpp import Llama
5
- import subprocess
6
- import sys
 
7
 
8
-
9
- subprocess.run([
10
- sys.executable, "-m", "pip", "install",
11
- "llama-cpp-python",
12
- "--extra-index-url", "https://abetlen.github.io/llama-cpp-python/whl/cpu",
13
- "--quiet"
14
- ], check=True)
15
-
16
- import gradio as gr
17
- import os
18
- from huggingface_hub import hf_hub_download
19
- from llama_cpp import Llama
20
  HF_TOKEN = os.environ.get("HF_TOKEN")
21
 
22
  SYSTEM_PROMPT = """Tu es l'assistant SAV officiel du Centre Chery Tunisie.
@@ -26,7 +15,7 @@ Réponds uniquement aux questions liées aux véhicules Chery."""
26
  print("⏳ Downloading GGUF model...")
27
  model_path = hf_hub_download(
28
  repo_id="dali4444444/chery-sav-chatbot-gguf",
29
- filename="chery-sav-chatbot-q4_k_m.gguf", # ⚠️ update after checking exact filename
30
  token=HF_TOKEN
31
  )
32
 
@@ -39,12 +28,14 @@ llm = Llama(
39
  )
40
  print("✅ Model ready!")
41
 
42
-
43
  def chat(message, history):
44
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
45
  for h in history:
46
- messages.append({"role": "user", "content": h[0]})
47
- messages.append({"role": "assistant", "content": h[1]})
 
 
 
48
  messages.append({"role": "user", "content": message})
49
 
50
  response = llm.create_chat_completion(
@@ -55,13 +46,18 @@ def chat(message, history):
55
  )
56
  return response["choices"][0]["message"]["content"]
57
 
 
58
 
59
- demo = gr.ChatInterface(
60
- fn=chat,
61
- title="🚗 Chery SAV Assistant",
62
- description="Posez vos questions sur vos véhicules Chery en français ou en arabe tunisien."
63
- )
 
 
 
 
 
64
 
65
  if __name__ == "__main__":
66
- port = int(os.environ.get("PORT", 7860))
67
- demo.launch(server_name="0.0.0.0", server_port=port)
 
2
  import os
3
  from huggingface_hub import hf_hub_download
4
  from llama_cpp import Llama
5
+ from fastapi import FastAPI
6
+ from pydantic import BaseModel
7
+ import uvicorn
8
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  HF_TOKEN = os.environ.get("HF_TOKEN")
10
 
11
  SYSTEM_PROMPT = """Tu es l'assistant SAV officiel du Centre Chery Tunisie.
 
15
  print("⏳ Downloading GGUF model...")
16
  model_path = hf_hub_download(
17
  repo_id="dali4444444/chery-sav-chatbot-gguf",
18
+ filename="chery-sav-chatbot-q4_k_m.gguf",
19
  token=HF_TOKEN
20
  )
21
 
 
28
  )
29
  print("✅ Model ready!")
30
 
 
31
  def chat(message, history):
32
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
33
  for h in history:
34
+ if isinstance(h, dict):
35
+ messages.append({"role": h["role"], "content": h["content"]})
36
+ else:
37
+ messages.append({"role": "user", "content": h[0]})
38
+ messages.append({"role": "assistant", "content": h[1]})
39
  messages.append({"role": "user", "content": message})
40
 
41
  response = llm.create_chat_completion(
 
46
  )
47
  return response["choices"][0]["message"]["content"]
48
 
49
+ app = FastAPI()
50
 
51
+ class ChatRequest(BaseModel):
52
+ message: str
53
+ history: list = []
54
+
55
+ @app.post("/api/chat")
56
+ async def api_chat(req: ChatRequest):
57
+ return {"reply": chat(req.message, req.history)}
58
+
59
+ demo = gr.ChatInterface(fn=chat, title="🚗 Chery SAV Assistant")
60
+ app = gr.mount_gradio_app(app, demo, path="/")
61
 
62
  if __name__ == "__main__":
63
+ uvicorn.run(app, host="0.0.0.0", port=7860)