yachty66 commited on
Commit
b278de8
1 Parent(s): a294fd2

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +3 -9
  2. app.py +62 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Aiclone
3
- emoji: 👁
4
- colorFrom: yellow
5
- colorTo: red
6
- sdk: gradio
7
- sdk_version: 4.8.0
8
  app_file: app.py
9
- pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: aiclone
 
 
 
 
 
3
  app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 4.7.1
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import time
3
+ from openai import OpenAI
4
+ from dotenv import load_dotenv
5
+ import os
6
+
7
+ load_dotenv()
8
+
9
+ with gr.Blocks() as demo:
10
+ chatbot = gr.Chatbot()
11
+ msg = gr.Textbox()
12
+ clear = gr.ClearButton([msg, chatbot])
13
+
14
+ def openai_request(messages):
15
+ """
16
+ requests to fine tuned openai model
17
+ """
18
+ client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
19
+ response = client.chat.completions.create(
20
+ model="ft:gpt-3.5-turbo-0613:personal::8SkHOPq1",
21
+ messages=messages,
22
+ temperature=0.5,
23
+ )
24
+ result = response.choices[0].message.content
25
+ return result
26
+
27
+ def l_to_openai_format(l):
28
+ """
29
+ converts a list of messages to open ai format messages
30
+ """
31
+ output = []
32
+ for i in range(len(l)):
33
+ if i % 2 == 0:
34
+ output.append({'role': 'user', 'content': l[i]})
35
+ else:
36
+ output.append({'role': 'assistant', 'content': l[i]})
37
+ return output
38
+
39
+ def message_and_history_to_list(message, history):
40
+ """
41
+ gets message and history and converts all to one list
42
+ """
43
+ l = []
44
+ for i in history:
45
+ for j in i:
46
+ l.append(j)
47
+ l.append(message)
48
+ return l
49
+
50
+ def respond(message, chat_history):
51
+ """
52
+ is called everytime a message is submitted into the chatbot
53
+ """
54
+ l = message_and_history_to_list(message, chat_history)
55
+ open_ai_request_format = l_to_openai_format(l)
56
+ bot_message = openai_request(open_ai_request_format)
57
+ chat_history.append((message, bot_message))
58
+ return "", chat_history
59
+
60
+ msg.submit(respond, [msg, chatbot], [msg, chatbot])
61
+
62
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ openai
3
+ python-dotenv