-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.py
116 lines (83 loc) · 2.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import argparse
import os
import gradio as gr
# import requests
from llama_cpp import Llama
from config import model_path
llm = Llama(
model_path = model_path,
chat_format = "llama-2",
n_gpu_layers = 30
)
url = 'http://localhost:8000/v1/chat/completions'
headers = {
'accept': 'application/json',
'Content-Type': 'application/json'
}
def do_ask(text):
res = llm.create_chat_completion(
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": f"{text}"
}
],
stream = True
)
all_text = ""
for chunk in res:
try:
res = chunk['choices'][0]["delta"]['content']
all_text += res
yield all_text
except Exception as e:
print(str(e))
pass
# def do_ask(text):
# data = {
# 'messages': [
# {
# 'content': 'You are a helpful assistant.',
# 'role': 'system'
# },
# {
# 'content': f'{text}',
# 'role': 'user'
# }
# ]
# }
# response = requests.post(url, headers=headers, json=data)
# print(response.json())
# print(response.json()['choices'][0]['message']['content'])
# return response.json()['choices'][0]['message']['content']
initial_md = """
webui作者:刘悦的技术博客 https://space.bilibili.com/3031494
模型作者:https://huggingface.co/tastypear/CausalLM-14B-DPO-alpha-GGUF
"""
with gr.Blocks() as app:
gr.Markdown(initial_md)
with gr.Accordion("对话输入"):
with gr.Row():
ori_video = gr.Textbox(label="请输入对话")
speech_button = gr.Button("发送")
with gr.Accordion("文本生成"):
with gr.Row():
with gr.Column():
text = gr.Textbox()
speech_button.click(do_ask,inputs=[ori_video],outputs=[text])
parser = argparse.ArgumentParser()
parser.add_argument(
"--server-name",
type=str,
default=None,
help="Server name for Gradio app",
)
parser.add_argument(
"--no-autolaunch",
action="store_true",
default=False,
help="Do not launch app automatically",
)
args = parser.parse_args()
app.launch(inbrowser=not args.no_autolaunch, server_name=args.server_name)