Spaces:
Runtime error
Runtime error
YOLO
Browse files
app.py
CHANGED
@@ -1,3 +1,35 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModel, AutoTokenizer
|
3 |
|
4 |
+
tokenizer = AutoTokenizer.from_pretrained("fnlp/moss-moon-003-sft-plugin", trust_remote_code=True)
|
5 |
+
model = AutoModel.from_pretrained("fnlp/moss-moon-003-sft-plugin", trust_remote_code=True).half().cuda()
|
6 |
+
model = model.eval()
|
7 |
+
|
8 |
+
def predict(input, history=None):
|
9 |
+
if history is None:
|
10 |
+
history = []
|
11 |
+
prompt = ""
|
12 |
+
for (q, r) in history:
|
13 |
+
prompt += "<|Human|>: " + q + "<eoh>\n"
|
14 |
+
prompt += "<|MOSS|>: " + r + "\n"
|
15 |
+
prompt += "<|Human|>: " + input + "<eoh>\n<|MOSS|>:"
|
16 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
17 |
+
outputs = model.generate(**inputs, do_sample=True, temperature=0.7, top_p=0.8, repetition_penalty=1.1, max_new_tokens=512)
|
18 |
+
response = tokenizer.decode(outputs[0])
|
19 |
+
completion = response[len(prompt)+2:]
|
20 |
+
history = history + [(input, completion)]
|
21 |
+
return history, history
|
22 |
+
|
23 |
+
|
24 |
+
with gr.Blocks() as demo:
|
25 |
+
gr.Markdown("moss-moon-003-sft-plugin")
|
26 |
+
state = gr.State([])
|
27 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=400)
|
28 |
+
with gr.Row():
|
29 |
+
with gr.Column(scale=4):
|
30 |
+
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
31 |
+
with gr.Column(scale=1):
|
32 |
+
button = gr.Button("Generate")
|
33 |
+
txt.submit(predict, [txt, state], [chatbot, state])
|
34 |
+
button.click(predict, [txt, state], [chatbot, state])
|
35 |
+
demo.queue().launch()
|