Spaces:
Sleeping
Sleeping
Aditya0619
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,69 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
|
4 |
-
# Initialize the model pipeline
|
5 |
chatbot_pipeline = pipeline("text-generation", model="Aditya0619/Medbot")
|
6 |
|
7 |
-
#
|
8 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
9 |
-
#
|
10 |
if history is None:
|
11 |
history = []
|
12 |
|
13 |
-
# Build
|
14 |
-
|
15 |
for user_input, bot_response in history:
|
16 |
-
|
17 |
-
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
# Generate a response using the chatbot pipeline
|
23 |
-
result = chatbot_pipeline(
|
24 |
-
conversation,
|
25 |
max_length=max_tokens,
|
26 |
temperature=temperature,
|
27 |
top_p=top_p,
|
28 |
-
pad_token_id=50256 #
|
29 |
-
)
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
|
34 |
-
#
|
35 |
-
history.append((message, bot_response))
|
36 |
return history, history
|
37 |
|
38 |
-
# Define the
|
39 |
with gr.Blocks() as demo:
|
40 |
-
#
|
41 |
-
gr.Markdown("# 🤖 AI Chatbot with Memory\nThis chatbot remembers your previous messages.")
|
42 |
|
43 |
-
# Input fields for
|
44 |
system_message = gr.Textbox(
|
45 |
label="System Message (Optional)",
|
46 |
placeholder="e.g., You are a helpful assistant."
|
47 |
)
|
48 |
-
max_tokens = gr.Slider(
|
49 |
-
|
50 |
-
)
|
51 |
-
temperature = gr.Slider(
|
52 |
-
label="Temperature", minimum=0.0, maximum=1.0, value=0.7, step=0.1
|
53 |
-
)
|
54 |
-
top_p = gr.Slider(
|
55 |
-
label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1
|
56 |
-
)
|
57 |
|
58 |
-
# Chatbot interface
|
59 |
chatbot = gr.Chatbot(label="Chat with AI")
|
60 |
-
user_input = gr.Textbox(label="Your Message", placeholder="Type a message...")
|
61 |
|
62 |
# Hidden state to store conversation history
|
63 |
state = gr.State([])
|
64 |
|
65 |
-
# Submit button to
|
66 |
submit = gr.Button("Send")
|
67 |
|
68 |
-
# Link the input and chatbot response function
|
69 |
submit.click(
|
70 |
respond,
|
71 |
inputs=[user_input, state, system_message, max_tokens, temperature, top_p],
|
72 |
outputs=[chatbot, state]
|
73 |
)
|
74 |
|
75 |
-
#
|
76 |
demo.load(lambda: [("Hi! How can I assist you today?", "")], outputs=chatbot)
|
77 |
|
78 |
# Launch the Gradio app
|
79 |
demo.launch()
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
|
4 |
+
# Initialize the conversational model pipeline
|
5 |
chatbot_pipeline = pipeline("text-generation", model="Aditya0619/Medbot")
|
6 |
|
7 |
+
# Function to manage history and generate responses
|
8 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
9 |
+
# Initialize history if it's None
|
10 |
if history is None:
|
11 |
history = []
|
12 |
|
13 |
+
# Build input by concatenating past messages (user-bot pairs)
|
14 |
+
chat_input = ""
|
15 |
for user_input, bot_response in history:
|
16 |
+
chat_input += f"User: {user_input}\nBot: {bot_response}\n"
|
17 |
+
chat_input += f"User: {message}\nBot:"
|
18 |
|
19 |
+
# Generate a response using the pipeline
|
20 |
+
response = chatbot_pipeline(
|
21 |
+
chat_input,
|
|
|
|
|
|
|
22 |
max_length=max_tokens,
|
23 |
temperature=temperature,
|
24 |
top_p=top_p,
|
25 |
+
pad_token_id=50256 # Avoids padding errors with models like GPT-2
|
26 |
+
)[0]["generated_text"].split("Bot:")[-1].strip()
|
27 |
|
28 |
+
# Update history with the new interaction
|
29 |
+
history.append((message, response))
|
30 |
|
31 |
+
# Return the updated chat history
|
|
|
32 |
return history, history
|
33 |
|
34 |
+
# Define the Gradio app layout
|
35 |
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("# 🤖 AI Chatbot with Memory\nChat with the bot and it will remember your conversation!")
|
|
|
37 |
|
38 |
+
# Input fields for settings
|
39 |
system_message = gr.Textbox(
|
40 |
label="System Message (Optional)",
|
41 |
placeholder="e.g., You are a helpful assistant."
|
42 |
)
|
43 |
+
max_tokens = gr.Slider(label="Max Tokens", minimum=50, maximum=500, value=250, step=10)
|
44 |
+
temperature = gr.Slider(label="Temperature", minimum=0.0, maximum=1.0, value=0.7, step=0.1)
|
45 |
+
top_p = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1)
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Chatbot interface and user input
|
48 |
chatbot = gr.Chatbot(label="Chat with AI")
|
49 |
+
user_input = gr.Textbox(label="Your Message", placeholder="Type a message...", lines=2)
|
50 |
|
51 |
# Hidden state to store conversation history
|
52 |
state = gr.State([])
|
53 |
|
54 |
+
# Submit button to send messages
|
55 |
submit = gr.Button("Send")
|
56 |
|
57 |
+
# Link the user input and chatbot response function
|
58 |
submit.click(
|
59 |
respond,
|
60 |
inputs=[user_input, state, system_message, max_tokens, temperature, top_p],
|
61 |
outputs=[chatbot, state]
|
62 |
)
|
63 |
|
64 |
+
# Initial greeting message
|
65 |
demo.load(lambda: [("Hi! How can I assist you today?", "")], outputs=chatbot)
|
66 |
|
67 |
# Launch the Gradio app
|
68 |
demo.launch()
|
69 |
+
|