rbx-imarcin commited on
Commit
2abdd24
·
verified ·
1 Parent(s): c418629

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -15
app.py CHANGED
@@ -1,26 +1,44 @@
1
- import gradio as gr
2
  import os
3
  import openai
 
4
 
5
  # Replace 'your_api_key_here' with your actual OpenAI API key
6
  api_token = os.getenv("oaikey")
7
- openai.api_key = api_token
8
 
9
- def ask_openai(prompt):
 
 
 
 
 
 
 
 
 
 
10
  response = openai.ChatCompletion.create(
11
- model="gpt-4", # or any other suitable model
12
- messages=[{"role": "user", "content": prompt}]
 
 
 
 
 
13
  )
14
- return response['choices'][0]['message']['content']
 
 
 
 
 
 
 
15
 
16
- # Creating the Gradio Interface
17
- iface = gr.Interface(
18
- fn=ask_openai,
19
- inputs="text",
20
- outputs="text",
21
- title="OpenAI Chatbot",
22
- description="A simple chatbot using OpenAI's GPT model."
23
- )
24
 
 
25
  if __name__ == "__main__":
26
- iface.launch()
 
 
 
1
  import os
2
  import openai
3
+ import gradio as gr
4
 
5
  # Replace 'your_api_key_here' with your actual OpenAI API key
6
  api_token = os.getenv("oaikey")
 
7
 
8
+ # Set your OpenAI API key
9
+ openai.api_key = os.getenv(api_token)
10
+
11
+ # Initialize chat history
12
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
13
+
14
+ def chatbot(user_input):
15
+ # Append user input to the message history
16
+ messages.append({"role": "user", "content": user_input})
17
+
18
+ # Generate response using OpenAI's GPT-3.5-turbo
19
  response = openai.ChatCompletion.create(
20
+ model="gpt-3.5-turbo",
21
+ messages=messages,
22
+ temperature=0.7,
23
+ max_tokens=150,
24
+ top_p=1,
25
+ frequency_penalty=0,
26
+ presence_penalty=0
27
  )
28
+
29
+ # Extract the assistant's reply
30
+ assistant_reply = response.choices[0].message["content"].strip()
31
+
32
+ # Append assistant's reply to the message history
33
+ messages.append({"role": "assistant", "content": assistant_reply})
34
+
35
+ return assistant_reply
36
 
37
+ # Create Gradio interface
38
+ with gr.Blocks() as demo:
39
+ chatbot_ui = gr.ChatInterface(fn=chatbot, title="GPT-3.5 Turbo Chatbot")
 
 
 
 
 
40
 
41
+ # Launch the Gradio app
42
  if __name__ == "__main__":
43
+ demo.launch()
44
+