yasserrmd commited on
Commit
8d44066
Β·
verified Β·
1 Parent(s): b269d57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -26
app.py CHANGED
@@ -1,41 +1,59 @@
1
  import os
2
- 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
- openai.api_key = XAI_API_KEY
 
 
 
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 with a humorous twist
13
- response = openai.ChatCompletion.create(
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 and return the response content with a hint of humor
21
  reply = response.choices[0].message["content"]
22
-
23
  # Add a humorous touch to the response
24
  if not reply.endswith("πŸ˜„"):
25
- reply += " πŸ˜„" # Ensure a smile at the end of each response
26
- return reply
 
 
 
27
 
28
  except Exception as e:
29
- return f"Oops, something went wrong! Just blame it on the robots... πŸ˜‰ (Error: {str(e)})"
30
-
31
- # Set up the Gradio interface
32
- interface = gr.Interface(
33
- fn=chat_with_buddy, # Function to call
34
- inputs="text", # Input type
35
- outputs="text", # Output type
36
- title="Chat Buddy", # Title for the interface
37
- description="Your friendly chatbot with a touch of humor! Ask Chat Buddy anything, and enjoy a chat that’s both helpful and light-hearted."
38
- )
 
 
 
 
 
 
 
 
 
 
39
 
40
- # Launch the app
41
- interface.launch()
 
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()