Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from llama_cpp import Llama
|
3 |
+
|
4 |
+
llm = Llama(model_path="model.gguf", n_ctx=4000, n_threads=2, chat_format="chatml")
|
5 |
+
|
6 |
+
def generate(message, history,temperature=0.3,max_tokens=512):
|
7 |
+
system_prompt = "You are Solar, an userful AI assistant."
|
8 |
+
formatted_prompt = [{"role": "system", "content": system_prompt}]
|
9 |
+
for user_prompt, bot_response in history:
|
10 |
+
formatted_prompt.append({"role": "user", "content": user_prompt})
|
11 |
+
formatted_prompt.append({"role": "assistant", "content": bot_response })
|
12 |
+
formatted_prompt.append({"role": "user", "content": message})
|
13 |
+
stream_response = llm.create_chat_completion(messages=formatted_prompt, temperature=temperature, max_tokens=max_tokens, stream=True)
|
14 |
+
response = ""
|
15 |
+
for chunk in stream_response:
|
16 |
+
if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
|
17 |
+
response += chunk['choices'][0]["delta"]["content"]
|
18 |
+
yield response
|
19 |
+
|
20 |
+
mychatbot = gr.Chatbot(
|
21 |
+
avatar_images=["user.png", "bots.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
|
22 |
+
|
23 |
+
iface = gr.ChatInterface(fn=generate, chatbot=mychatbot, retry_btn=None, undo_btn=None)
|
24 |
+
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
gr.HTML("<center><h1>Tomoniai's Chat with Solar 10.7b</h1></center>")
|
27 |
+
iface.render()
|
28 |
+
|
29 |
+
demo.queue().launch(show_api=False, server_name="0.0.0.0")
|
30 |
+
|