AWS-ANSWER-BOT commited on
Commit
262ccd9
·
verified ·
1 Parent(s): f4f88e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -133
app.py CHANGED
@@ -1,116 +1,75 @@
1
  import gradio as gr
2
  import os
3
  import sys
4
- import json
5
  import requests
6
  import random
7
 
8
-
9
  MODEL = "gpt-4.1-mini"
10
  API_URL = os.getenv("API_URL")
11
  DISABLED = os.getenv("DISABLED") == 'True'
12
  OPENAI_API_KEYS = os.getenv("OPENAI_API_KEYS").split(',')
13
- print (API_URL)
14
- print (OPENAI_API_KEYS)
15
- NUM_THREADS = int(os.getenv("NUM_THREADS"))
16
-
17
- print (NUM_THREADS)
18
 
19
  def exception_handler(exception_type, exception, traceback):
20
- print("%s: %s" % (exception_type.__name__, exception))
21
  sys.excepthook = exception_handler
22
  sys.tracebacklimit = 0
23
-
24
- def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:gr.Request):
25
  payload = {
26
  "model": MODEL,
27
- "messages": [{"role": "user", "content": f"{inputs}"}],
28
  "temperature": temperature,
29
  "top_p": top_p,
30
- "n" : 1,
31
  "stream": True,
32
- "presence_penalty":0,
33
- "frequency_penalty":0,
34
  }
35
  OPENAI_API_KEY = random.choice(OPENAI_API_KEYS)
36
- print (OPENAI_API_KEY)
37
-
38
- headers_dict = {key.decode('utf-8'): value.decode('utf-8') for key, value in request.headers.raw}
39
-
40
  headers = {
41
  "Content-Type": "application/json",
42
  "Authorization": f"Bearer {OPENAI_API_KEY}",
43
- "Headers": f"{headers_dict}"
44
  }
45
 
46
- # print(f"chat_counter - {chat_counter}")
47
- if chat_counter != 0 :
48
  messages = []
49
  for i, data in enumerate(history):
50
- if i % 2 == 0:
51
- role = 'user'
52
- else:
53
- role = 'assistant'
54
- message = {}
55
- message["role"] = role
56
- message["content"] = data
57
- messages.append(message)
58
-
59
- message = {}
60
- message["role"] = "user"
61
- message["content"] = inputs
62
- messages.append(message)
63
- payload = {
64
- "model": MODEL,
65
- "messages": messages,
66
- "temperature" : temperature,
67
- "top_p": top_p,
68
- "n" : 1,
69
- "stream": True,
70
- "presence_penalty":0,
71
- "frequency_penalty":0,
72
- }
73
 
74
  chat_counter += 1
75
-
76
  history.append(inputs)
77
- token_counter = 0
78
- partial_words = ""
79
  counter = 0
80
 
81
  try:
82
- # make a POST request to the API endpoint using the requests.post method, passing in stream=True
83
  response = requests.post(API_URL, headers=headers, json=payload, stream=True)
84
- response_code = f"{response}"
85
- #if response_code.strip() != "<Response [200]>":
86
- # #print(f"response code - {response}")
87
- # raise Exception(f"Sorry, hitting rate limit. Please try again later. {response}")
88
-
89
  for chunk in response.iter_lines():
90
- #print (chunk)
91
- #sys.stdout.flush()
92
- #Skipping first chunk
93
  if counter == 0:
94
  counter += 1
95
  continue
96
- #counter+=1
97
- # check whether each line is non-empty
98
- if chunk.decode() :
99
- chunk = chunk.decode()
100
- # decode each line as response data is in bytes
101
- if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
102
- partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
103
  if token_counter == 0:
104
  history.append(" " + partial_words)
105
  else:
106
  history[-1] = partial_words
107
  token_counter += 1
108
- yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ], history, chat_counter, response, gr.update(interactive=False), gr.update(interactive=False) # resembles {chatbot: chat, state: history}
109
  except Exception as e:
110
- print (f'error found: {e}')
111
- yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ], history, chat_counter, response, gr.update(interactive=True), gr.update(interactive=True)
112
- print(json.dumps({"chat_counter": chat_counter, "payload": payload, "partial_words": partial_words, "token_counter": token_counter, "counter": counter}))
113
-
114
 
115
  def reset_textbox():
116
  return gr.update(value='', interactive=False), gr.update(interactive=False)
@@ -118,82 +77,31 @@ def reset_textbox():
118
  title = """<h1 align="center">GPT-4.1 mini: Research Preview (Short-Term Availability)</h1>"""
119
  if DISABLED:
120
  title = """<h1 align="center" style="color:red">This app has reached OpenAI's usage limit. Please check back tomorrow.</h1>"""
121
- description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form:
122
- ```
123
- User: <utterance>
124
- Assistant: <utterance>
125
- User: <utterance>
126
- Assistant: <utterance>
127
- ...
128
- ```
129
- In this app, you can explore the outputs of a gpt-4 turbo LLM.
130
- """
131
 
132
- theme = gr.themes.Default(primary_hue="green")
133
 
134
- with gr.Blocks(css = """#col_container { margin-left: auto; margin-right: auto;}
135
- #chatbot {height: 520px; overflow: auto;}""",
136
- theme=theme) as demo:
137
  gr.HTML(title)
138
  gr.HTML("""<h3 align="center">This app provides you full access to GPT-4.1 mini (1M token limit). You don't need any OPENAI API key.</h3>""")
139
- gr.HTML("""
140
- <div style="padding: 12px; margin-bottom: 16px; border-radius: 6px; background-color: #e6f4ea; text-align: center;">
141
- 🚀 <b>Try our new ChatAnnotator</b> powered by Cohere's <b>Command-A</b> model!
142
- It offers a much-improved interface allowing you to <b>highlight and annotate specific errors</b> directly within chatbot responses, and even prompt immediate corrections.
143
- <a href="https://chatannotator.com" target="_blank">Click here to check it out!</a>
144
- </div>
145
- """)
146
-
147
 
148
- #gr.HTML('''<center><a href="https://huggingface.co/spaces/ysharma/ChatGPT4?duplicate=true"><img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a>Duplicate the Space and run securely with your OpenAI API Key</center>''')
149
- with gr.Column(elem_id = "col_container", visible=False) as main_block:
150
- #GPT4 API Key is provided by Huggingface
151
- #openai_api_key = gr.Textbox(type='password', label="Enter only your GPT4 OpenAI API key here")
152
- chatbot = gr.Chatbot(elem_id='chatbot') #c
153
- inputs = gr.Textbox(placeholder= "Hi there!", label= "Type an input and press Enter") #t
154
- state = gr.State([]) #s
155
  with gr.Row():
156
  with gr.Column(scale=7):
157
- b1 = gr.Button(visible=not DISABLED) #.style(full_width=True)
158
  with gr.Column(scale=3):
159
- server_status_code = gr.Textbox(label="Status code from OpenAI server", )
160
-
161
- #inputs, top_p, temperature, top_k, repetition_penalty
162
  with gr.Accordion("Parameters", open=False):
163
- top_p = gr.Slider( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
164
- temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
165
- #top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
166
- #repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
167
  chat_counter = gr.Number(value=0, visible=False, precision=0)
168
-
169
- with gr.Column(elem_id = "user_consent_container") as user_consent_block:
170
- # Get user consent
171
- accept_checkbox = gr.Checkbox(visible=False)
172
- js = "(x) => confirm('By clicking \"OK\", I agree that my data may be published or shared.')"
173
- with gr.Accordion("User Consent for Data Collection, Use, and Sharing", open=True):
174
- gr.HTML("""
175
- <div>
176
- <p>By using our app, which is powered by OpenAI's API, you acknowledge and agree to the following terms regarding the data you provide:</p>
177
- <ol>
178
- <li><strong>Collection:</strong> We may collect information, including the inputs you type into our app, the outputs generated by OpenAI's API, and certain technical details about your device and connection (such as browser type, operating system, and IP address) provided by your device's request headers.</li>
179
- <li><strong>Use:</strong> We may use the collected data for research purposes, to improve our services, and to develop new products or services, including commercial applications, and for security purposes, such as protecting against unauthorized access and attacks.</li>
180
- <li><strong>Sharing and Publication:</strong> Your data, including the technical details collected from your device's request headers, may be published, shared with third parties, or used for analysis and reporting purposes.</li>
181
- <li><strong>Data Retention:</strong> We may retain your data, including the technical details collected from your device's request headers, for as long as necessary.</li>
182
- </ol>
183
- <p>By continuing to use our app, you provide your explicit consent to the collection, use, and potential sharing of your data as described above. If you do not agree with our data collection, use, and sharing practices, please do not use our app.</p>
184
- </div>
185
- """)
186
- accept_button = gr.Button("I Agree")
187
-
188
- def enable_inputs():
189
- return gr.update(visible=False), gr.update(visible=True)
190
-
191
- accept_button.click(None, None, accept_checkbox, js=js, queue=False)
192
- accept_checkbox.change(fn=enable_inputs, inputs=[], outputs=[user_consent_block, main_block], queue=False)
193
 
194
  inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
195
- inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
196
  b1.click(reset_textbox, [], [inputs, b1], queue=False)
197
- b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, server_status_code, inputs, b1],) #openai_api_key
198
-
199
  demo.queue(max_size=10, default_concurrency_limit=NUM_THREADS, api_open=False).launch(share=False)
 
1
  import gradio as gr
2
  import os
3
  import sys
4
+ import json
5
  import requests
6
  import random
7
 
 
8
  MODEL = "gpt-4.1-mini"
9
  API_URL = os.getenv("API_URL")
10
  DISABLED = os.getenv("DISABLED") == 'True'
11
  OPENAI_API_KEYS = os.getenv("OPENAI_API_KEYS").split(',')
12
+ NUM_THREADS = int(os.getenv("NUM_THREADS", "1"))
 
 
 
 
13
 
14
  def exception_handler(exception_type, exception, traceback):
15
+ print(f"{exception_type.__name__}: {exception}")
16
  sys.excepthook = exception_handler
17
  sys.tracebacklimit = 0
18
+
19
+ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request: gr.Request):
20
  payload = {
21
  "model": MODEL,
22
+ "messages": [{"role": "user", "content": inputs}],
23
  "temperature": temperature,
24
  "top_p": top_p,
25
+ "n": 1,
26
  "stream": True,
27
+ "presence_penalty": 0,
28
+ "frequency_penalty": 0,
29
  }
30
  OPENAI_API_KEY = random.choice(OPENAI_API_KEYS)
31
+
 
 
 
32
  headers = {
33
  "Content-Type": "application/json",
34
  "Authorization": f"Bearer {OPENAI_API_KEY}",
 
35
  }
36
 
37
+ if chat_counter != 0:
 
38
  messages = []
39
  for i, data in enumerate(history):
40
+ role = "user" if i % 2 == 0 else "assistant"
41
+ messages.append({"role": role, "content": data})
42
+ messages.append({"role": "user", "content": inputs})
43
+ payload["messages"] = messages
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  chat_counter += 1
 
46
  history.append(inputs)
47
+ partial_words = ""
48
+ token_counter = 0
49
  counter = 0
50
 
51
  try:
 
52
  response = requests.post(API_URL, headers=headers, json=payload, stream=True)
 
 
 
 
 
53
  for chunk in response.iter_lines():
 
 
 
54
  if counter == 0:
55
  counter += 1
56
  continue
57
+ if chunk:
58
+ chunk_str = chunk.decode()
59
+ chunk_json = json.loads(chunk_str[6:])
60
+ delta = chunk_json['choices'][0]['delta']
61
+ if "content" in delta:
62
+ partial_words += delta["content"]
 
63
  if token_counter == 0:
64
  history.append(" " + partial_words)
65
  else:
66
  history[-1] = partial_words
67
  token_counter += 1
68
+ yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)], history, chat_counter, gr.update(interactive=False), gr.update(interactive=False)
69
  except Exception as e:
70
+ print(f"Error: {e}")
71
+
72
+ yield [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)], history, chat_counter, gr.update(interactive=True), gr.update(interactive=True)
 
73
 
74
  def reset_textbox():
75
  return gr.update(value='', interactive=False), gr.update(interactive=False)
 
77
  title = """<h1 align="center">GPT-4.1 mini: Research Preview (Short-Term Availability)</h1>"""
78
  if DISABLED:
79
  title = """<h1 align="center" style="color:red">This app has reached OpenAI's usage limit. Please check back tomorrow.</h1>"""
80
+ description = """Language models can be conditioned to act like dialogue agents through a conversational prompt."""
 
 
 
 
 
 
 
 
 
81
 
82
+ theme = gr.themes.Default(primary_hue="green")
83
 
84
+ with gr.Blocks(css="#col_container { margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}", theme=theme) as demo:
 
 
85
  gr.HTML(title)
86
  gr.HTML("""<h3 align="center">This app provides you full access to GPT-4.1 mini (1M token limit). You don't need any OPENAI API key.</h3>""")
 
 
 
 
 
 
 
 
87
 
88
+ with gr.Column(elem_id="col_container", visible=not DISABLED) as main_block:
89
+ chatbot = gr.Chatbot(elem_id='chatbot')
90
+ inputs = gr.Textbox(placeholder="Hi there!", label="Type an input and press Enter")
91
+ state = gr.State([])
 
 
 
92
  with gr.Row():
93
  with gr.Column(scale=7):
94
+ b1 = gr.Button(visible=not DISABLED)
95
  with gr.Column(scale=3):
96
+ server_status_code = gr.Textbox(label="Status code from OpenAI server")
 
 
97
  with gr.Accordion("Parameters", open=False):
98
+ top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)")
99
+ temperature = gr.Slider(minimum=0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature")
 
 
100
  chat_counter = gr.Number(value=0, visible=False, precision=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
103
+ inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, inputs, b1])
104
  b1.click(reset_textbox, [], [inputs, b1], queue=False)
105
+ b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state], [chatbot, state, chat_counter, inputs, b1])
106
+
107
  demo.queue(max_size=10, default_concurrency_limit=NUM_THREADS, api_open=False).launch(share=False)