Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# pipe = pipeline("text-generation", model="tiiuae/falcon-40b-instruct", trust_remote_code=True)
|
5 |
+
|
6 |
+
def format_chat_prompt(message, chat_history, instruction):
|
7 |
+
prompt = f"System:{instruction}"
|
8 |
+
for turn in chat_history:
|
9 |
+
user_message, bot_message = turn
|
10 |
+
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
11 |
+
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
12 |
+
return prompt
|
13 |
+
|
14 |
+
def respond(message, chat_history, instruction, temperature=0.7):
|
15 |
+
prompt = format_chat_prompt(message, chat_history, instruction)
|
16 |
+
chat_history = chat_history + [[message, ""]]
|
17 |
+
# stream = client.generate_stream(prompt,
|
18 |
+
# max_new_tokens=1024,
|
19 |
+
# stop_sequences=["\nUser:", "<|endoftext|>"],
|
20 |
+
# temperature=temperature)
|
21 |
+
#stop_sequences to not generate the user answer
|
22 |
+
acc_text = ""
|
23 |
+
#Streaming the tokens
|
24 |
+
for idx, response in enumerate(stream):
|
25 |
+
text_token = response.token.text
|
26 |
+
|
27 |
+
if response.details:
|
28 |
+
return
|
29 |
+
|
30 |
+
if idx == 0 and text_token.startswith(" "):
|
31 |
+
text_token = text_token[1:]
|
32 |
+
|
33 |
+
acc_text += text_token
|
34 |
+
last_turn = list(chat_history.pop(-1))
|
35 |
+
last_turn[-1] += acc_text
|
36 |
+
chat_history = chat_history + [last_turn]
|
37 |
+
yield "", chat_history
|
38 |
+
acc_text = ""
|
39 |
+
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
chatbot = gr.Chatbot(height=240) #just to fit the notebook
|
42 |
+
msg = gr.Textbox(label="Prompt")
|
43 |
+
with gr.Accordion(label="Advanced options",open=False):
|
44 |
+
system = gr.Textbox(label="System message", lines=2, value="A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers.")
|
45 |
+
temperature = gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)
|
46 |
+
btn = gr.Button("Submit")
|
47 |
+
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
48 |
+
|
49 |
+
btn.click(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot])
|
50 |
+
msg.submit(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot]) #Press enter to submit
|
51 |
+
gr.close_all()
|
52 |
+
demo.queue().launch(share=True, server_port=int(os.environ['PORT4']))
|