Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,27 @@
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
for turn in chat_history:
|
9 |
user_message, bot_message = turn
|
10 |
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
@@ -12,7 +29,7 @@ def format_chat_prompt(message, chat_history, instruction):
|
|
12 |
return prompt
|
13 |
|
14 |
def respond(message, chat_history, instruction, temperature=0.7):
|
15 |
-
|
16 |
chat_history = chat_history + [[message, ""]]
|
17 |
# stream = client.generate_stream(prompt,
|
18 |
# max_new_tokens=1024,
|
@@ -36,17 +53,35 @@ def respond(message, chat_history, instruction, temperature=0.7):
|
|
36 |
chat_history = chat_history + [last_turn]
|
37 |
yield "", chat_history
|
38 |
acc_text = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
with
|
41 |
-
chatbot = gr.Chatbot(height=240)
|
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])
|
51 |
|
52 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import spaces
|
3 |
+
from typing import List, Tuple
|
4 |
from transformers import pipeline
|
5 |
|
6 |
+
# Initialize Model
|
7 |
# pipe = pipeline("text-generation", model="tiiuae/falcon-40b-instruct", trust_remote_code=True)
|
8 |
|
9 |
+
def format_chat_prompt(message: str, chat_history: List[Tuple[str, str]], instruction: str) -> str:
|
10 |
+
"""
|
11 |
+
Formats a chat prompt for a conversational AI model by incorporating the system instruction,
|
12 |
+
previous chat history, and the new user message.
|
13 |
+
|
14 |
+
Args:
|
15 |
+
message (str): The latest user message to be included in the prompt.
|
16 |
+
chat_history (List[Tuple[str, str]]): A list of tuples where each tuple represents
|
17 |
+
a previous conversation turn, with the user's message and the assistant's response.
|
18 |
+
instruction (str): A system-level instruction that guides the assistant's behavior
|
19 |
+
(e.g., helpful, formal, or humorous).
|
20 |
+
|
21 |
+
Returns:
|
22 |
+
str: The formatted chat prompt that includes the instruction, chat history, and new user message.
|
23 |
+
"""
|
24 |
+
prompt = f"System: {instruction}"
|
25 |
for turn in chat_history:
|
26 |
user_message, bot_message = turn
|
27 |
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
|
|
29 |
return prompt
|
30 |
|
31 |
def respond(message, chat_history, instruction, temperature=0.7):
|
32 |
+
formatted_prompt = format_chat_prompt(message, chat_history, instruction)
|
33 |
chat_history = chat_history + [[message, ""]]
|
34 |
# stream = client.generate_stream(prompt,
|
35 |
# max_new_tokens=1024,
|
|
|
53 |
chat_history = chat_history + [last_turn]
|
54 |
yield "", chat_history
|
55 |
acc_text = ""
|
56 |
+
|
57 |
+
####### GRADIO APP #######
|
58 |
+
title = """<h1 id="title"> Falcon-40B-Instruct-Chatbot </h1>"""
|
59 |
+
|
60 |
+
description = """
|
61 |
+
- The model used as the AI Assistant [Falcon-40B-Instruct-Chat](https://huggingface.co/tiiuae/falcon-40b-instruct).
|
62 |
+
"""
|
63 |
+
|
64 |
+
css = '''
|
65 |
+
h1#title {
|
66 |
+
text-align: center;
|
67 |
+
}
|
68 |
+
'''
|
69 |
+
|
70 |
+
theme = gr.themes.Soft()
|
71 |
+
demo = gr.Blocks(css=css, theme=theme)
|
72 |
|
73 |
+
with demo:
|
74 |
+
chatbot = gr.Chatbot(height=240)
|
75 |
msg = gr.Textbox(label="Prompt")
|
76 |
+
|
77 |
with gr.Accordion(label="Advanced options",open=False):
|
78 |
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.")
|
79 |
temperature = gr.Slider(label="temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)
|
80 |
+
|
81 |
btn = gr.Button("Submit")
|
82 |
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
83 |
|
84 |
btn.click(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot])
|
85 |
+
msg.submit(respond, inputs=[msg, chatbot, system], outputs=[msg, chatbot])
|
86 |
|
87 |
demo.launch()
|