iremkrc commited on
Commit
cf28f2a
1 Parent(s): f80d361

Add app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ import openai
4
+ import os
5
+ from dotenv import load_dotenv
6
+
7
+ # Load environment variables from the .env file
8
+ load_dotenv()
9
+
10
+ openai.api_key = os.environ["openai_api_key"]
11
+ #openai.api_key = os.environ.__getattribute__("openai_api_key")
12
+ messages = [ {"role": "system", "content":
13
+ "You are a intelligent assistant."} ]
14
+ chat = openai.ChatCompletion.create(
15
+ model="gpt-3.5-turbo",
16
+ messages=[
17
+ {"role": "system", "content": "You are a helpful assistant."}
18
+ ]
19
+ )
20
+
21
+ with gr.Blocks() as demo:
22
+ chatbot = gr.Chatbot()
23
+ msg = gr.Textbox()
24
+ clear = gr.Button("Clear")
25
+
26
+ def user(user_message, history):
27
+ messages.append(
28
+ {"role": "user", "content": user_message},
29
+ )
30
+ return "", history + [[user_message, None]]
31
+
32
+ def bot(history):
33
+ chat = openai.ChatCompletion.create(
34
+ model="gpt-3.5-turbo", messages=messages
35
+ )
36
+ bot_message = chat.choices[0].message.content
37
+ messages.append({"role": "assistant", "content": bot_message})
38
+ history[-1][1] = ""
39
+ for character in bot_message:
40
+ history[-1][1] += character
41
+ time.sleep(0.05)
42
+ yield history
43
+
44
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
45
+ bot, chatbot, chatbot
46
+ )
47
+ clear.click(lambda: None, None, chatbot, queue=False)
48
+
49
+ demo.queue()
50
+ demo.launch()