CodeNine commited on
Commit
e503c3b
·
verified ·
1 Parent(s): 070fd85

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import openai
4
+ import gradio as gr
5
+
6
+ # Load API key from environment variable
7
+ openai.api_key = os.getenv("OPENAI_API_KEY")
8
+
9
+ def openai_chat(message, history):
10
+ messages = [{"role": "system", "content": "You are a helpful assistant."}]
11
+ for user, bot in history:
12
+ messages.append({"role": "user", "content": user})
13
+ messages.append({"role": "assistant", "content": bot})
14
+ messages.append({"role": "user", "content": message})
15
+
16
+ response = openai.ChatCompletion.create(
17
+ model="gpt-4", # Or "gpt-3.5-turbo"
18
+ messages=messages,
19
+ temperature=0.7,
20
+ )
21
+ return response.choices[0].message["content"]
22
+
23
+ demo = gr.ChatInterface(
24
+ fn=openai_chat,
25
+ title="OpenAI Chatbot",
26
+ description="Chat with GPT-4 using the OpenAI API",
27
+ theme=gr.themes.Soft()
28
+ )
29
+
30
+ if __name__ == "__main__":
31
+ demo.launch()