falethera commited on
Commit
a7131af
1 Parent(s): 7d093af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -8
app.py CHANGED
@@ -1,18 +1,52 @@
1
  import openai
2
  import gradio as gr
 
3
 
4
- openai.api_key = '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'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- messages = [{"role": "system", "content": "You are an expert that specializes in providing advices on durian, delicious food and restuarants in Malaysia and Singapore."}]
7
  def CustomChatGPT(user_input):
8
  messages.append({"role": "user", "content": user_input})
9
- response = openai.ChatCompletion.create(
10
- model = "gpt-3.5-turbo",
11
- messages = messages
12
- )
 
 
 
 
 
 
 
 
 
 
13
  ChatGPT_reply = response["choices"][0]["message"]["content"]
14
  messages.append({"role": "assistant", "content": ChatGPT_reply})
 
 
 
15
  return ChatGPT_reply
16
 
17
- demo = gradio.Interface(fn=CustomChatGPT, inputs = "text", outputs = "text", title = "Hello, I am an AI chatbot for Female Power Living. Ask me any questions on tenancy rights in New South Wales.")
18
- demo.launch(share=True)
 
 
 
 
 
 
1
  import openai
2
  import gradio as gr
3
+ import json
4
 
5
+ openai.api_key = "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"
6
+
7
+ def save_conversation():
8
+ with open('conversation.json', 'w') as f:
9
+ json.dump(messages, f)
10
+
11
+ def load_conversation():
12
+ try:
13
+ with open('conversation.json', 'r') as f:
14
+ return json.load(f)
15
+ except FileNotFoundError:
16
+ return []
17
+
18
+ messages = load_conversation()
19
+
20
+ if not messages:
21
+ messages.append({"role": "system", "content": "You are a knowledgeable assistant specialized in providing assistance regarding tenancy rights and regulation in New South Wales, Australia."})
22
 
 
23
  def CustomChatGPT(user_input):
24
  messages.append({"role": "user", "content": user_input})
25
+
26
+ # Ensure the conversation fits within the model's maximum token limit
27
+ conversation = messages[-4096:]
28
+
29
+ try:
30
+ response = openai.ChatCompletion.create(
31
+ model="gpt-3.5-turbo",
32
+ messages=conversation,
33
+ max_tokens=1000,
34
+ temperature=0.7)
35
+ except openai.api_resources.request_error.RequestError as e:
36
+ print(f"Received error from OpenAI: {e}")
37
+ return "I'm sorry, but I'm unable to generate a response at this time."
38
+
39
  ChatGPT_reply = response["choices"][0]["message"]["content"]
40
  messages.append({"role": "assistant", "content": ChatGPT_reply})
41
+
42
+ save_conversation()
43
+
44
  return ChatGPT_reply
45
 
46
+ interface = gr.Interface(fn=CustomChatGPT,
47
+ inputs="textbox",
48
+ outputs="textbox",
49
+ title="HR HELPER",
50
+ description="Chat with an AI assistant that can answer questions about tenancy rights in New South Wales, Australia.")
51
+
52
+ interface.launch()