Mikecode123 commited on
Commit
943bfe3
·
verified ·
1 Parent(s): 5cbd13a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -18
app.py CHANGED
@@ -1,32 +1,63 @@
1
- from fastapi import FastAPI
2
- from pydantic import BaseModel
3
  from llama_cpp import Llama
 
4
 
5
- app = FastAPI()
 
 
 
 
 
 
 
6
 
7
- # Load model once on startup
8
  llm = Llama(
9
- model_path="qwen2-1_5b-instruct-q4_0.gguf",
10
  n_ctx=2048,
11
  n_threads=2
12
  )
13
 
14
- # Request body structure
15
- class ChatRequest(BaseModel):
16
- message: str
 
 
 
 
 
 
 
17
 
18
- @app.get("/")
19
- def home():
20
- return {"status": "AI server running"}
21
 
22
- @app.post("/chat")
23
- def chat(req: ChatRequest):
24
- output = llm.create_completion(
25
- prompt=req.message,
26
  max_tokens=300,
27
  temperature=0.7
28
  )
29
 
30
- return {
31
- "response": output["choices"][0]["text"].strip()
32
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
  from llama_cpp import Llama
4
+ from huggingface_hub import hf_hub_download
5
 
6
+ # =========================
7
+ # LOAD MODEL
8
+ # =========================
9
+ model_path = hf_hub_download(
10
+ repo_id="Mikecode123/ALX",
11
+ filename="qwen2-1_5b-instruct-q4_0.gguf",
12
+ token=os.getenv("HF_TOKEN")
13
+ )
14
 
 
15
  llm = Llama(
16
+ model_path=model_path,
17
  n_ctx=2048,
18
  n_threads=2
19
  )
20
 
21
+ # =========================
22
+ # CHAT FUNCTION (FIXED)
23
+ # =========================
24
+ def chat(message, history):
25
+ # convert Gradio tuple history -> messages format
26
+ messages = []
27
+
28
+ for user, bot in history:
29
+ messages.append({"role": "user", "content": user})
30
+ messages.append({"role": "assistant", "content": bot})
31
 
32
+ messages.append({"role": "user", "content": message})
 
 
33
 
34
+ output = llm.create_chat_completion(
35
+ messages=messages,
 
 
36
  max_tokens=300,
37
  temperature=0.7
38
  )
39
 
40
+ reply = output["choices"][0]["message"]["content"]
41
+
42
+ history.append((message, reply))
43
+
44
+ return history, history
45
+
46
+ # =========================
47
+ # UI (FIXED GRADIO 4)
48
+ # =========================
49
+ with gr.Blocks() as demo:
50
+ gr.Markdown("# 🧠 Living Legend AI Chat")
51
+
52
+ chatbot = gr.Chatbot()
53
+ msg = gr.Textbox()
54
+
55
+ state = gr.State([])
56
+
57
+ def respond(message, history):
58
+ new_history, updated_state = chat(message, history)
59
+ return "", new_history, updated_state
60
+
61
+ msg.submit(respond, [msg, state], [msg, chatbot, state])
62
+
63
+ demo.launch()