Spaces:
Runtime error
Runtime error
update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,38 @@
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
+
def format_chat_prompt(message, chat_history,instruction):
|
4 |
+
prompt = f"System:{instruction}"
|
5 |
+
for turn in chat_history:
|
6 |
+
user_message, bot_message = turn
|
7 |
+
prompt = f"{prompt}\nUser: {user_message}\nAssistant:{bot_message}"
|
8 |
+
prompt= f"{prompt}\nUser: {message}\nAssistant:"
|
9 |
+
return prompt
|
10 |
+
|
11 |
+
def respond(message, chat_history,instruction,model,temperature=0.7):
|
12 |
+
if model == "Llama2-Chat":
|
13 |
+
model = "meta-llama/Llama-2-7b-chat-hf"
|
14 |
+
else:
|
15 |
+
model="mistralai/Mistral-7B-Instruct-v0.1"
|
16 |
+
|
17 |
+
client = InferenceClient(model=f"{model}",token=API_KEY,timeout=30)
|
18 |
+
formatted_prompt = format_chat_prompt(message,chat_history,instruction)
|
19 |
+
bot_message = client.text_generation(formatted_prompt, max_new_tokens=1024,
|
20 |
+
stop_sequences=["\nUser:", "<|endoftext|>"],
|
21 |
+
temperature=temperature)
|
22 |
+
chat_history.append((message,bot_message))
|
23 |
+
return "",chat_history
|
24 |
+
|
25 |
+
with gr.Blocks() as demo:
|
26 |
+
chatbot = gr.Chatbot(height=240) #visual param
|
27 |
+
msg = gr.Textbox(label='prompt')
|
28 |
+
with gr.Accordion(label="Advanced options",open=False):
|
29 |
+
model = gr.Radio(["MistralAI"])
|
30 |
+
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.")
|
31 |
+
temperature = gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)
|
32 |
+
btn = gr.Button("Submit")
|
33 |
+
clear = gr.ClearButton(components=[msg,chatbot], value="Clear console")
|
34 |
+
|
35 |
+
btn.click(respond, inputs=[msg,chatbot,system,model], outputs=[msg, chatbot])
|
36 |
+
msg.submit(respond, inputs=[msg, chatbot,system,model], outputs=[msg, chatbot])
|
37 |
+
|
38 |
+
demo.queue().launch()
|