Spaces:
Sleeping
Sleeping
Aditya0619
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -10,7 +10,7 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
10 |
if history is None:
|
11 |
history = []
|
12 |
|
13 |
-
# Build input by concatenating past messages
|
14 |
chat_input = ""
|
15 |
for user_input, bot_response in history:
|
16 |
chat_input += f"User: {user_input}\nBot: {bot_response}\n"
|
@@ -22,10 +22,10 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
22 |
max_length=max_tokens,
|
23 |
temperature=temperature,
|
24 |
top_p=top_p,
|
25 |
-
pad_token_id=50256 #
|
26 |
)[0]["generated_text"].split("Bot:")[-1].strip()
|
27 |
|
28 |
-
# Update
|
29 |
history.append((message, response))
|
30 |
|
31 |
# Return the updated chat history
|
@@ -33,35 +33,43 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
33 |
|
34 |
# Define the Gradio app layout
|
35 |
with gr.Blocks() as demo:
|
36 |
-
gr.Markdown("# 🤖 AI Chatbot with Memory\nChat with
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
-
# Chatbot
|
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
|
55 |
submit = gr.Button("Send")
|
56 |
|
57 |
-
#
|
58 |
submit.click(
|
59 |
respond,
|
60 |
inputs=[user_input, state, system_message, max_tokens, temperature, top_p],
|
61 |
outputs=[chatbot, state]
|
62 |
)
|
63 |
|
64 |
-
#
|
65 |
demo.load(lambda: [("Hi! How can I assist you today?", "")], outputs=chatbot)
|
66 |
|
67 |
# Launch the Gradio app
|
|
|
10 |
if history is None:
|
11 |
history = []
|
12 |
|
13 |
+
# Build input by concatenating past messages
|
14 |
chat_input = ""
|
15 |
for user_input, bot_response in history:
|
16 |
chat_input += f"User: {user_input}\nBot: {bot_response}\n"
|
|
|
22 |
max_length=max_tokens,
|
23 |
temperature=temperature,
|
24 |
top_p=top_p,
|
25 |
+
pad_token_id=50256 # Avoid padding issues for models like GPT-2 variants
|
26 |
)[0]["generated_text"].split("Bot:")[-1].strip()
|
27 |
|
28 |
+
# Update the conversation history
|
29 |
history.append((message, response))
|
30 |
|
31 |
# Return the updated chat history
|
|
|
33 |
|
34 |
# Define the Gradio app layout
|
35 |
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("# 🤖 AI Chatbot with Memory\nChat with me! I’ll remember your messages.")
|
37 |
|
38 |
+
# Taskbar with configurable parameters
|
39 |
+
with gr.Row():
|
40 |
+
with gr.Accordion("⚙️ Configure Chatbot Settings", open=False):
|
41 |
+
system_message = gr.Textbox(
|
42 |
+
label="System Message (Optional)",
|
43 |
+
placeholder="e.g., You are a helpful assistant."
|
44 |
+
)
|
45 |
+
max_tokens = gr.Slider(
|
46 |
+
label="Max Tokens", minimum=50, maximum=500, value=250, step=10
|
47 |
+
)
|
48 |
+
temperature = gr.Slider(
|
49 |
+
label="Temperature", minimum=0.0, maximum=1.0, value=0.7, step=0.1
|
50 |
+
)
|
51 |
+
top_p = gr.Slider(
|
52 |
+
label="Top P", minimum=0.0, maximum=1.0, value=0.9, step=0.1
|
53 |
+
)
|
54 |
|
55 |
+
# Chatbot and user input section
|
56 |
chatbot = gr.Chatbot(label="Chat with AI")
|
57 |
user_input = gr.Textbox(label="Your Message", placeholder="Type a message...", lines=2)
|
58 |
|
59 |
+
# Hidden state to store the conversation history
|
60 |
state = gr.State([])
|
61 |
|
62 |
+
# Submit button
|
63 |
submit = gr.Button("Send")
|
64 |
|
65 |
+
# Connect user input to the chatbot response function
|
66 |
submit.click(
|
67 |
respond,
|
68 |
inputs=[user_input, state, system_message, max_tokens, temperature, top_p],
|
69 |
outputs=[chatbot, state]
|
70 |
)
|
71 |
|
72 |
+
# Display an initial greeting message
|
73 |
demo.load(lambda: [("Hi! How can I assist you today?", "")], outputs=chatbot)
|
74 |
|
75 |
# Launch the Gradio app
|