Spaces:
Runtime error
Runtime error
Commit
·
c2ee33a
1
Parent(s):
a62d423
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,62 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
from pathlib import Path
|
4 |
+
import argparse
|
5 |
+
model_file = "yi-6b.Q4_0.gguf"
|
6 |
+
if not os.path.isfile(model_file):
|
7 |
+
os.system("wget -c https://huggingface.co/zgce/Yi-6B-Chat-spicy_GGUF/blob/main/Yi-6B-Chat-spicy_Q6_K.gguf")
|
8 |
|
9 |
+
DEFAULT_MODEL_PATH = model_file
|
10 |
+
|
11 |
+
from llama_cpp import Llama
|
12 |
+
llm = Llama(model_path=model_file, model_type="mistral")
|
13 |
+
|
14 |
+
|
15 |
+
def predict(input, chatbot, max_length, top_p, temperature, history):
|
16 |
+
chatbot.append((input, ""))
|
17 |
+
response = ""
|
18 |
+
history.append(input)
|
19 |
+
|
20 |
+
for output in llm(input, stream=True, temperature=temperature, top_p=top_p, max_tokens=max_length, ):
|
21 |
+
piece = output['choices'][0]['text']
|
22 |
+
response += piece
|
23 |
+
chatbot[-1] = (chatbot[-1][0], response)
|
24 |
+
|
25 |
+
yield chatbot, history
|
26 |
+
|
27 |
+
history.append(response)
|
28 |
+
yield chatbot, history
|
29 |
+
|
30 |
+
|
31 |
+
def reset_user_input():
|
32 |
+
return gr.update(value="")
|
33 |
+
|
34 |
+
|
35 |
+
def reset_state():
|
36 |
+
return [], []
|
37 |
+
|
38 |
+
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
gr.HTML("""<h1 align="center">Yi-6B-GGUF by llama-cpp-python</h1>""")
|
41 |
+
|
42 |
+
chatbot = gr.Chatbot()
|
43 |
+
with gr.Row():
|
44 |
+
with gr.Column(scale=4):
|
45 |
+
user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=8)
|
46 |
+
submitBtn = gr.Button("Submit", variant="primary")
|
47 |
+
with gr.Column(scale=1):
|
48 |
+
max_length = gr.Slider(0, 32048, value=2048, step=1.0, label="Maximum Length", interactive=True)
|
49 |
+
top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True)
|
50 |
+
temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)
|
51 |
+
emptyBtn = gr.Button("Clear History")
|
52 |
+
|
53 |
+
history = gr.State([])
|
54 |
+
|
55 |
+
submitBtn.click(
|
56 |
+
predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history], show_progress=True
|
57 |
+
)
|
58 |
+
submitBtn.click(reset_user_input, [], [user_input])
|
59 |
+
|
60 |
+
emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)
|
61 |
+
|
62 |
+
demo.queue().launch(share=False, inbrowser=True)
|