akazmi commited on
Commit
e5ed350
·
verified ·
1 Parent(s): e1faf31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -27
app.py CHANGED
@@ -1,38 +1,29 @@
1
  import gradio as gr
2
  import openai
3
 
4
- # Set your OpenAI API key
5
  openai.api_key = "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"
6
 
7
- # Function to generate response from OpenAI's GPT model
8
- def respond(message, conversation_history=[]):
9
- # Append user input to the conversation history
10
- conversation_history.append({"role": "user", "content": message})
11
-
12
- # Call the OpenAI API for a response
13
- response = openai.ChatCompletion.create(
14
- model="gpt-3.5-turbo",
15
- messages=conversation_history
16
- )
17
-
18
- # Extract the chatbot's response
19
- bot_response = response['choices'][0]['message']['content']
20
-
21
- # Append the bot's response to the conversation history
22
- conversation_history.append({"role": "assistant", "content": bot_response})
23
-
24
- # Return the bot's response
25
- return bot_response
26
 
27
- # Gradio interface setup
28
  iface = gr.Interface(
29
- fn=respond,
30
- inputs=gr.Textbox(placeholder="Type your message here..."),
31
- outputs=gr.Textbox(),
32
  title="Real-Time Chatbot with GPT-3.5",
33
  description="Ask any question or start a conversation. Powered by OpenAI GPT-3.5-turbo."
34
  )
35
 
36
- # Launch the Gradio interface
37
- if __name__ == "__main__":
38
- iface.launch()
 
1
  import gradio as gr
2
  import openai
3
 
4
+ # Set your OpenAI API key here
5
  openai.api_key = "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"
6
 
7
+ def generate_response(message):
8
+ try:
9
+ # Call the OpenAI API to get a response
10
+ response = openai.ChatCompletion.create(
11
+ model="gpt-3.5-turbo",
12
+ messages=[{"role": "user", "content": message}],
13
+ max_tokens=150
14
+ )
15
+ return response['choices'][0]['message']['content'].strip()
16
+ except Exception as e:
17
+ return str(e)
 
 
 
 
 
 
 
 
18
 
19
+ # Create the Gradio interface
20
  iface = gr.Interface(
21
+ fn=generate_response,
22
+ inputs=gr.inputs.Textbox(placeholder="Type your message here...", label="Message"),
23
+ outputs="text",
24
  title="Real-Time Chatbot with GPT-3.5",
25
  description="Ask any question or start a conversation. Powered by OpenAI GPT-3.5-turbo."
26
  )
27
 
28
+ # Launch the interface
29
+ iface.launch()