Spaces:
Runtime error
Runtime error
k3ybladewielder
commited on
Commit
•
264b602
1
Parent(s):
73b635d
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
task = "text-generation" # Specify the task as text generation
|
5 |
+
# model = "tiiuae/falcon-7b-instruct" # Use a specific model (option 1) or
|
6 |
+
model = "tiiuae/falcon-40b-instruct" # Use another specific model (option 2)
|
7 |
+
get_completion = pipeline(task=task, model=model, trust_remote_code=True) # Create a text generation pipeline
|
8 |
+
|
9 |
+
# Define a function to format the chat history and create a prompt for the chatbot
|
10 |
+
def format_chat_prompt(message, chat_history):
|
11 |
+
prompt = ""
|
12 |
+
for turn in chat_history:
|
13 |
+
user_message, bot_message = turn
|
14 |
+
prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
|
15 |
+
prompt = f"{prompt}\nUser: {message}\nAssistant:"
|
16 |
+
return prompt
|
17 |
+
|
18 |
+
# Define a function to respond to the user's message
|
19 |
+
def respond(message, chat_history):
|
20 |
+
formatted_prompt = format_chat_prompt(message, chat_history)
|
21 |
+
# Generate a response using the specified model, given the formatted prompt
|
22 |
+
bot_message = get_completion(formatted_prompt,
|
23 |
+
max_new_tokens=1024,
|
24 |
+
# stop_sequences=["\nUser:", "<|endoftext|>"],
|
25 |
+
stop_sequences=["\nUser:", ""]).generated_text
|
26 |
+
chat_history.append((message, bot_message)) # Add the user and bot messages to the chat history
|
27 |
+
return "", chat_history
|
28 |
+
|
29 |
+
# Create a Gradio interface with UI components
|
30 |
+
with gr.Blocks() as demo:
|
31 |
+
chatbot = gr.Chatbot(height=240) # Create a chatbot UI component
|
32 |
+
msg = gr.Textbox(label="Prompt") # Create a textbox UI component for user input
|
33 |
+
btn = gr.Button("Submit") # Create a submit button UI component
|
34 |
+
# Create a clear button UI component that clears the console
|
35 |
+
clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
|
36 |
+
|
37 |
+
# Define the behavior of the submit button when clicked
|
38 |
+
btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
39 |
+
# Define the behavior of the enter key in the textbox (submit action)
|
40 |
+
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
41 |
+
|
42 |
+
# Launch the Gradio interface
|
43 |
+
demo.launch()
|