Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,36 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
|
| 5 |
+
# Load GGUF model
|
| 6 |
+
MODEL_PATH = "./models/mistral.gguf"
|
| 7 |
|
| 8 |
+
llm = Llama(
|
| 9 |
+
model_path=MODEL_PATH,
|
| 10 |
+
n_ctx=2048,
|
| 11 |
+
n_threads=9, # Increase for more speed if CPU allows
|
| 12 |
+
n_batch=128,
|
| 13 |
+
use_mlock=True,
|
| 14 |
+
use_mmap=True,
|
| 15 |
+
verbose=False
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
# Streaming generator
|
| 19 |
+
def generate_response(prompt):
|
| 20 |
+
stream = llm(
|
| 21 |
+
prompt=f"[INST] {prompt.strip()} [/INST]",
|
| 22 |
+
max_tokens=512,
|
| 23 |
+
stop=["</s>"],
|
| 24 |
+
stream=True
|
| 25 |
+
)
|
| 26 |
+
partial = ""
|
| 27 |
+
for chunk in stream:
|
| 28 |
+
partial += chunk["choices"][0]["text"]
|
| 29 |
+
yield partial
|
| 30 |
+
|
| 31 |
+
# Gradio UI
|
| 32 |
+
gr.ChatInterface(
|
| 33 |
+
fn=generate_response,
|
| 34 |
+
title="Leo9 AI Tutor",
|
| 35 |
+
description="An ai chatbots who answer any question.",
|
| 36 |
+
).launch()
|