Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,59 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# Initialize OpenAI client with Groq model
|
| 6 |
XAI_API_KEY = os.getenv("XAI_API_KEY")
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
# Define a function to handle chat with humor
|
| 10 |
-
def chat_with_buddy(user_message):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
-
# Generate a response from Groq
|
| 13 |
-
response =
|
| 14 |
model="grok-beta",
|
| 15 |
-
messages=
|
| 16 |
-
{"role": "system", "content": "You are Chat Buddy, a friendly and humorous chatbot who loves to make people smile."},
|
| 17 |
-
{"role": "user", "content": user_message},
|
| 18 |
-
]
|
| 19 |
)
|
| 20 |
-
# Extract
|
| 21 |
reply = response.choices[0].message["content"]
|
| 22 |
-
|
| 23 |
# Add a humorous touch to the response
|
| 24 |
if not reply.endswith("๐"):
|
| 25 |
-
reply += " ๐"
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
except Exception as e:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# Launch the
|
| 41 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
# Initialize OpenAI client with Groq model
|
| 6 |
XAI_API_KEY = os.getenv("XAI_API_KEY")
|
| 7 |
+
client = OpenAI(
|
| 8 |
+
api_key=XAI_API_KEY,
|
| 9 |
+
base_url="https://api.x.ai/v1",
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
# Define a function to handle chat with humor, including chat history
|
| 13 |
+
def chat_with_buddy(history, user_message):
|
| 14 |
+
# Start the conversation with a system message and add previous chat history
|
| 15 |
+
messages = [{"role": "system", "content": "You are Chat Buddy, a friendly and humorous chatbot who loves to make people smile."}]
|
| 16 |
+
messages += [{"role": "user", "content": msg[0]} for msg in history] + [{"role": "assistant", "content": msg[1]} for msg in history if len(msg) > 1]
|
| 17 |
+
messages.append({"role": "user", "content": user_message})
|
| 18 |
+
|
| 19 |
try:
|
| 20 |
+
# Generate a response from Groq
|
| 21 |
+
response = client.ChatCompletion.create(
|
| 22 |
model="grok-beta",
|
| 23 |
+
messages=messages
|
|
|
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
+
# Extract the response content
|
| 26 |
reply = response.choices[0].message["content"]
|
| 27 |
+
|
| 28 |
# Add a humorous touch to the response
|
| 29 |
if not reply.endswith("๐"):
|
| 30 |
+
reply += " ๐"
|
| 31 |
+
|
| 32 |
+
# Update chat history with the new user message and assistant response
|
| 33 |
+
history.append((user_message, reply))
|
| 34 |
+
return history
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
+
# In case of error, add error message to history
|
| 38 |
+
error_message = f"Oops, something went wrong! Just blame it on the robots... ๐ (Error: {str(e)})"
|
| 39 |
+
history.append((user_message, error_message))
|
| 40 |
+
return history
|
| 41 |
+
|
| 42 |
+
# Set up the Gradio chat interface with history
|
| 43 |
+
with gr.Blocks() as chat_interface:
|
| 44 |
+
gr.Markdown("# Chat Buddy")
|
| 45 |
+
gr.Markdown("Your friendly chatbot with a touch of humor! Ask Chat Buddy anything, and enjoy a chat thatโs both helpful and light-hearted.")
|
| 46 |
+
|
| 47 |
+
chatbot = gr.Chatbot()
|
| 48 |
+
message = gr.Textbox(placeholder="Type your message here...", label="Your Message")
|
| 49 |
+
submit_button = gr.Button("Send")
|
| 50 |
+
|
| 51 |
+
# Define the submit action to update chat history
|
| 52 |
+
def submit(user_message, chat_history):
|
| 53 |
+
return chat_with_buddy(chat_history, user_message), ""
|
| 54 |
+
|
| 55 |
+
# Link the submit button to the function
|
| 56 |
+
submit_button.click(submit, inputs=[message, chatbot], outputs=[chatbot, message])
|
| 57 |
|
| 58 |
+
# Launch the chat interface
|
| 59 |
+
chat_interface.launch()
|