Gopala Krishna commited on
Commit
34564f3
1 Parent(s): dfee6f8
.vs/MyChatGPTTurbo/FileContentIndex/{ab91987c-a512-4d01-b8b5-27fcada31777.vsidx → c69d4add-0ca9-42c0-9da9-fec814f20f6f.vsidx} RENAMED
File without changes
.vs/MyChatGPTTurbo/FileContentIndex/feff3d0f-367e-40ad-b46b-abeffe69c7f7.vsidx DELETED
Binary file (10.2 kB)
 
.vs/MyChatGPTTurbo/v17/.wsuo CHANGED
Binary files a/.vs/MyChatGPTTurbo/v17/.wsuo and b/.vs/MyChatGPTTurbo/v17/.wsuo differ
 
.vs/ProjectSettings.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "CurrentProjectSetting": null
3
+ }
.vs/slnx.sqlite CHANGED
Binary files a/.vs/slnx.sqlite and b/.vs/slnx.sqlite differ
 
app.py CHANGED
@@ -1,9 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
- import openai
3
  import gradio as gr
 
 
 
4
 
5
  try:
6
- openai.api_key = os.environ["OPENAI_API_KEY"]
7
 
8
  except KeyError:
9
  error_message = "System is at capacity right now.Please try again later"
@@ -15,30 +54,120 @@ else:
15
  {"role": "system", "content": "My AI Assistant"},
16
  ]
17
 
18
- def chatbot(input):
19
- try:
20
- if input:
21
- messages.append({"role": "user", "content": input})
22
- chat = openai.ChatCompletion.create(
23
- model="gpt-3.5-turbo", messages=messages
24
- )
25
- reply = chat.choices[0].message.content
26
- messages.append({"role": "assistant", "content": reply})
27
- return reply
28
- except openai.error.OpenAIError as e:
29
- return "System is at capacity right now.Please try again later"
30
 
31
- #iface = gr.Interface(
32
- # fn=chatbot,
33
- # inputs=gr.inputs.Textbox(lines=7, label="Query"),
34
- # outputs=gr.outputs.Textbox(label="Response"),
35
- # theme=gr.themes.Default(primary_hue="sky"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- iface = gr.Interface(
38
- fn=chatbot,
39
- inputs=gr.inputs.Textbox(lines=7, label="Query"),
40
- outputs=gr.outputs.Textbox(label="Response"),
41
- theme=gr.themes.Default(primary_hue="slate"))
42
 
43
 
44
- iface.launch()
 
1
+ #import os
2
+ #import openai
3
+ #import gradio as gr
4
+
5
+ #try:
6
+ # openai.api_key = os.environ["OPENAI_API_KEY"]
7
+
8
+ #except KeyError:
9
+ # error_message = "System is at capacity right now.Please try again later"
10
+ # print(error_message)
11
+ # def chatbot(input):
12
+ # return error_message
13
+ #else:
14
+ # messages = [
15
+ # {"role": "system", "content": "My AI Assistant"},
16
+ # ]
17
+
18
+ #def chatbot(input):
19
+ # try:
20
+ # if input:
21
+ # messages.append({"role": "user", "content": input})
22
+ # chat = openai.ChatCompletion.create(
23
+ # model="gpt-3.5-turbo", messages=messages
24
+ # )
25
+ # reply = chat.choices[0].message.content
26
+ # messages.append({"role": "assistant", "content": reply})
27
+ # return reply
28
+ # except openai.error.OpenAIError as e:
29
+ # return "System is at capacity right now.Please try again later"
30
+ #iface = gr.Interface(
31
+ # fn=chatbot,
32
+ # inputs=gr.inputs.Textbox(lines=7, label="Query"),
33
+ # outputs=gr.outputs.Textbox(label="Response"),
34
+ # theme=gr.themes.Default(primary_hue="slate"))
35
+ #iface.launch()
36
+
37
+
38
  import os
 
39
  import gradio as gr
40
+ import json
41
+ import requests
42
+ import openai
43
 
44
  try:
45
+ openai.api_key = os.environ["OPENAI_API_KEY"]
46
 
47
  except KeyError:
48
  error_message = "System is at capacity right now.Please try again later"
 
54
  {"role": "system", "content": "My AI Assistant"},
55
  ]
56
 
57
+ #Streaming endpoint for OPENAI ChatGPT
58
+ API_URL = "https://api.openai.com/v1/chat/completions"
59
+ top_p_chatgpt = 1.0
60
+ temperature_chatgpt = 1.0
 
 
 
 
 
 
 
 
61
 
62
+ #Predict function for CHATGPT
63
+ def chatbot(inputs, chat_counter_chatgpt, chatbot_chatgpt=[], history=[]):
64
+ #Define payload and header for chatgpt API
65
+ payload = {
66
+ "model": "gpt-3.5-turbo",
67
+ "messages": [{"role": "user", "content": f"{inputs}"}],
68
+ "temperature" : 1.0,
69
+ "top_p":1.0,
70
+ "n" : 1,
71
+ "stream": True,
72
+ "presence_penalty":0,
73
+ "frequency_penalty":0,
74
+ }
75
+
76
+ headers = {
77
+ "Content-Type": "application/json",
78
+ "Authorization": f"Bearer {openai.api_key}"
79
+ }
80
+
81
+ #Handling the different roles for ChatGPT
82
+ if chat_counter_chatgpt != 0 :
83
+ messages=[]
84
+ for data in chatbot_chatgpt:
85
+ temp1 = {}
86
+ temp1["role"] = "user"
87
+ temp1["content"] = data[0]
88
+ temp2 = {}
89
+ temp2["role"] = "assistant"
90
+ temp2["content"] = data[1]
91
+ messages.append(temp1)
92
+ messages.append(temp2)
93
+ temp3 = {}
94
+ temp3["role"] = "user"
95
+ temp3["content"] = inputs
96
+ messages.append(temp3)
97
+ payload = {
98
+ "model": "gpt-3.5-turbo",
99
+ "messages": messages, #[{"role": "user", "content": f"{inputs}"}],
100
+ "temperature" : temperature_chatgpt, #1.0,
101
+ "top_p": top_p_chatgpt, #1.0,
102
+ "n" : 1,
103
+ "stream": True,
104
+ "presence_penalty":0,
105
+ "frequency_penalty":0,
106
+ }
107
+
108
+ chat_counter_chatgpt+=1
109
+
110
+ history.append("You asked: "+ inputs)
111
+
112
+ # make a POST request to the API endpoint using the requests.post method, passing in stream=True
113
+ response = requests.post(API_URL, headers=headers, json=payload, stream=True)
114
+ token_counter = 0
115
+ partial_words = ""
116
+
117
+ counter=0
118
+ for chunk in response.iter_lines():
119
+ #Skipping the first chunk
120
+ if counter == 0:
121
+ counter+=1
122
+ continue
123
+ # check whether each line is non-empty
124
+ if chunk.decode() :
125
+ chunk = chunk.decode()
126
+ # decode each line as response data is in bytes
127
+ if len(chunk) > 13 and "content" in json.loads(chunk[6:])['choices'][0]["delta"]:
128
+ partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
129
+ if token_counter == 0:
130
+ history.append(" " + partial_words)
131
+ else:
132
+ history[-1] = partial_words
133
+ chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
134
+ token_counter+=1
135
+ yield chat, history, chat_counter_chatgpt # this resembles {chatbot: chat, state: history}
136
+
137
+
138
+ def reset_textbox():
139
+ return gr.update(value="")
140
+
141
+ def reset_chat(chatbot, state):
142
+ return None, []
143
+
144
+ with gr.Blocks(css="""#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
145
+ #chatgpt {height: 700px; overflow: auto;}} """, theme=gr.themes.Default(primary_hue="slate") ) as demo:
146
+ with gr.Row():
147
+ with gr.Column(scale=14):
148
+ with gr.Box():
149
+ with gr.Row():
150
+ with gr.Column(scale=13):
151
+ inputs = gr.Textbox(label="Ask anything ⤵️ " )
152
+ with gr.Column(scale=1):
153
+ b1 = gr.Button('Submit', elem_id = 'submit').style(full_width=True)
154
+ b2 = gr.Button('Clear', elem_id = 'clear').style(full_width=True)
155
+ state_chatgpt = gr.State([])
156
+
157
+ with gr.Box():
158
+ with gr.Row():
159
+ chatbot_chatgpt = gr.Chatbot(elem_id="chatgpt", label='')
160
+ chat_counter_chatgpt = gr.Number(value=0, visible=False, precision=0)
161
+
162
+
163
+ inputs.submit(reset_textbox, [], [inputs])
164
+
165
+ b1.click( chatbot,
166
+ [ inputs, chat_counter_chatgpt, chatbot_chatgpt, state_chatgpt],
167
+ [chatbot_chatgpt, state_chatgpt],)
168
+
169
+ b2.click(reset_chat, [chatbot_chatgpt, state_chatgpt], [chatbot_chatgpt, state_chatgpt])
170
 
171
+ demo.queue(concurrency_count=16).launch(height= 2500, debug=True)
 
 
 
 
172
 
173