LuxOAI commited on
Commit
06b1340
1 Parent(s): 502b80d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ messages = load_conversation()
18
+
19
+ if not messages:
20
+ messages.append({"role": "system", "content": "You are a super computer coding assistant, capable of solving any programming problem. You are equipped with the knowledge of various programming languages and tools, and can assist beginners in quickly creating incredibly powerful code."})
21
+
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 a specialized assistant that can answer questions about recruiting, hiring, and various HR and CRM tools. Developed by A. Leschik.")
51
+
52
+ interface.launch()