Update app.py
Browse files
app.py
CHANGED
@@ -59,41 +59,83 @@ def get_chat_response(api_key, user_input, history):
|
|
59 |
history = history + [[user_input, assistant_reply]]
|
60 |
return history, history
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
# Define the Gradio Blocks interface
|
63 |
with gr.Blocks() as demo:
|
64 |
gr.Markdown(
|
65 |
"""
|
66 |
-
# Fireworks AI Chatbot
|
67 |
Interact with the Fireworks AI chatbot powered by the LLaMA model.
|
68 |
Enter your **Fireworks AI API key** and your messages below to receive responses in real-time.
|
69 |
"""
|
70 |
)
|
71 |
|
72 |
with gr.Row():
|
73 |
-
with gr.Column(scale=
|
74 |
api_key_input = gr.Textbox(
|
75 |
-
label="Fireworks AI API Key",
|
76 |
placeholder="Enter your API key here...",
|
77 |
type="password",
|
78 |
lines=1
|
79 |
)
|
80 |
-
with gr.Column(scale=
|
81 |
set_key_button = gr.Button("Set API Key")
|
82 |
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
with gr.Row():
|
86 |
-
with gr.Column(scale=
|
87 |
user_input = gr.Textbox(
|
88 |
placeholder="Type your message here...",
|
89 |
-
label="Your Message",
|
90 |
lines=1
|
91 |
)
|
92 |
-
with gr.Column(scale=
|
93 |
send_button = gr.Button("Send")
|
94 |
|
95 |
# Optional: Add example buttons
|
96 |
-
with gr.Accordion("Examples", open=False):
|
97 |
examples = gr.Examples(
|
98 |
examples=[
|
99 |
"Tell me a joke.",
|
@@ -108,70 +150,35 @@ with gr.Blocks() as demo:
|
|
108 |
# Initialize the conversation history and store the API key
|
109 |
history = gr.State([])
|
110 |
stored_api_key = gr.State("")
|
111 |
-
|
112 |
-
def set_api_key(api_key):
|
113 |
-
"""
|
114 |
-
Stores the user's API key.
|
115 |
-
|
116 |
-
Args:
|
117 |
-
api_key (str): User-provided Fireworks AI API key.
|
118 |
-
|
119 |
-
Returns:
|
120 |
-
str: Confirmation message.
|
121 |
-
"""
|
122 |
-
if api_key:
|
123 |
-
return "API Key has been set."
|
124 |
-
else:
|
125 |
-
return "Please enter a valid API key."
|
126 |
-
|
127 |
# Set API Key Button Click Event
|
128 |
set_key_button.click(
|
129 |
fn=set_api_key,
|
130 |
inputs=api_key_input,
|
131 |
-
outputs=
|
132 |
-
_js="() => {}" # No output component to update
|
133 |
)
|
134 |
|
135 |
-
def send_message(user_input, history, api_key):
|
136 |
-
"""
|
137 |
-
Handles sending messages to the chatbot.
|
138 |
-
|
139 |
-
Args:
|
140 |
-
user_input (str): The message input by the user.
|
141 |
-
history (list): The conversation history.
|
142 |
-
api_key (str): User-provided Fireworks AI API key.
|
143 |
-
|
144 |
-
Returns:
|
145 |
-
tuple: Updated conversation history with the assistant's response.
|
146 |
-
"""
|
147 |
-
if not api_key:
|
148 |
-
assistant_reply = "Please enter your Fireworks AI API key above."
|
149 |
-
history = history + [[user_input, assistant_reply]]
|
150 |
-
return history, history
|
151 |
-
|
152 |
-
return get_chat_response(api_key, user_input, history)
|
153 |
-
|
154 |
# Define the function to handle the interaction
|
155 |
send_button.click(
|
156 |
fn=send_message,
|
157 |
-
inputs=[user_input, history,
|
158 |
outputs=[chatbot, history]
|
159 |
)
|
160 |
|
161 |
# Allow pressing Enter to send the message
|
162 |
user_input.submit(
|
163 |
fn=send_message,
|
164 |
-
inputs=[user_input, history,
|
165 |
outputs=[chatbot, history]
|
166 |
)
|
167 |
|
168 |
gr.Markdown(
|
169 |
"""
|
170 |
---
|
171 |
-
|
172 |
"""
|
173 |
)
|
174 |
|
175 |
# Launch the interface
|
176 |
if __name__ == "__main__":
|
177 |
-
demo.launch()
|
|
|
59 |
history = history + [[user_input, assistant_reply]]
|
60 |
return history, history
|
61 |
|
62 |
+
def set_api_key(api_key):
|
63 |
+
"""
|
64 |
+
Stores the user's API key.
|
65 |
+
|
66 |
+
Args:
|
67 |
+
api_key (str): User-provided Fireworks AI API key.
|
68 |
+
|
69 |
+
Returns:
|
70 |
+
tuple: Stored API key and confirmation message.
|
71 |
+
"""
|
72 |
+
if api_key:
|
73 |
+
return api_key, "✅ API Key has been set."
|
74 |
+
else:
|
75 |
+
return "", "❌ Please enter a valid API key."
|
76 |
+
|
77 |
+
def send_message(user_input, history, api_key):
|
78 |
+
"""
|
79 |
+
Handles sending messages to the chatbot.
|
80 |
+
|
81 |
+
Args:
|
82 |
+
user_input (str): The message input by the user.
|
83 |
+
history (list): The conversation history.
|
84 |
+
api_key (str): User-provided Fireworks AI API key.
|
85 |
+
|
86 |
+
Returns:
|
87 |
+
tuple: Updated conversation history with the assistant's response.
|
88 |
+
"""
|
89 |
+
if not api_key:
|
90 |
+
assistant_reply = "❌ Please enter your Fireworks AI API key above."
|
91 |
+
history = history + [[user_input, assistant_reply]]
|
92 |
+
return history, history
|
93 |
+
|
94 |
+
return get_chat_response(api_key, user_input, history)
|
95 |
+
|
96 |
# Define the Gradio Blocks interface
|
97 |
with gr.Blocks() as demo:
|
98 |
gr.Markdown(
|
99 |
"""
|
100 |
+
# 🪄 Fireworks AI Chatbot
|
101 |
Interact with the Fireworks AI chatbot powered by the LLaMA model.
|
102 |
Enter your **Fireworks AI API key** and your messages below to receive responses in real-time.
|
103 |
"""
|
104 |
)
|
105 |
|
106 |
with gr.Row():
|
107 |
+
with gr.Column(scale=7):
|
108 |
api_key_input = gr.Textbox(
|
109 |
+
label="🔑 Fireworks AI API Key",
|
110 |
placeholder="Enter your API key here...",
|
111 |
type="password",
|
112 |
lines=1
|
113 |
)
|
114 |
+
with gr.Column(scale=3):
|
115 |
set_key_button = gr.Button("Set API Key")
|
116 |
|
117 |
+
# Output for API key confirmation
|
118 |
+
api_key_status = gr.Textbox(
|
119 |
+
label="API Key Status",
|
120 |
+
interactive=False,
|
121 |
+
lines=1,
|
122 |
+
value=""
|
123 |
+
)
|
124 |
+
|
125 |
+
chatbot = gr.Chatbot(label="💬 Chatbot")
|
126 |
|
127 |
with gr.Row():
|
128 |
+
with gr.Column(scale=8):
|
129 |
user_input = gr.Textbox(
|
130 |
placeholder="Type your message here...",
|
131 |
+
label="📝 Your Message",
|
132 |
lines=1
|
133 |
)
|
134 |
+
with gr.Column(scale=2):
|
135 |
send_button = gr.Button("Send")
|
136 |
|
137 |
# Optional: Add example buttons
|
138 |
+
with gr.Accordion("💡 Examples", open=False):
|
139 |
examples = gr.Examples(
|
140 |
examples=[
|
141 |
"Tell me a joke.",
|
|
|
150 |
# Initialize the conversation history and store the API key
|
151 |
history = gr.State([])
|
152 |
stored_api_key = gr.State("")
|
153 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
# Set API Key Button Click Event
|
155 |
set_key_button.click(
|
156 |
fn=set_api_key,
|
157 |
inputs=api_key_input,
|
158 |
+
outputs=[stored_api_key, api_key_status]
|
|
|
159 |
)
|
160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
# Define the function to handle the interaction
|
162 |
send_button.click(
|
163 |
fn=send_message,
|
164 |
+
inputs=[user_input, history, stored_api_key],
|
165 |
outputs=[chatbot, history]
|
166 |
)
|
167 |
|
168 |
# Allow pressing Enter to send the message
|
169 |
user_input.submit(
|
170 |
fn=send_message,
|
171 |
+
inputs=[user_input, history, stored_api_key],
|
172 |
outputs=[chatbot, history]
|
173 |
)
|
174 |
|
175 |
gr.Markdown(
|
176 |
"""
|
177 |
---
|
178 |
+
**ℹ️ Note**: Ensure you have a valid Fireworks AI API key. You can obtain one from [Fireworks AI](https://fireworks.ai/).
|
179 |
"""
|
180 |
)
|
181 |
|
182 |
# Launch the interface
|
183 |
if __name__ == "__main__":
|
184 |
+
demo.launch()
|