AWS-ANSWER-BOT commited on
Commit
a485c36
·
verified ·
1 Parent(s): 37cfa3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -41
app.py CHANGED
@@ -1,41 +1,60 @@
1
- In this app, you can explore the outputs of a gpt-4 turbo LLM.
2
- """
3
-
4
- theme = gr.themes.Default(primary_hue="green")
5
-
6
- with gr.Blocks(css="""#col_container { margin-left: auto; margin-right: auto;}
7
- #chatbot {height: 520px; overflow: auto;}""",
8
- theme=theme) as demo:
9
- gr.HTML(title)
10
- 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>""")
11
- gr.HTML("""
12
- <div style="padding: 12px; margin-bottom: 16px; border-radius: 6px; background-color: #e6f4ea; text-align: center;">
13
- 🚀 <b>Try our new ChatAnnotator</b> powered by Cohere's <b>Command-A</b> model!
14
- 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.
15
- <a href="https://chatannotator.com" target="_blank">Click here to check it out!</a>
16
- </div>
17
- """)
18
-
19
- with gr.Column(elem_id="col_container", visible=not DISABLED) as main_block:
20
- chatbot = gr.Chatbot(elem_id='chatbot')
21
- inputs = gr.Textbox(placeholder="Hi there!", label="Type an input and press Enter")
22
- state = gr.State([])
23
- with gr.Row():
24
- with gr.Column(scale=7):
25
- b1 = gr.Button(visible=not DISABLED)
26
- with gr.Column(scale=3):
27
- server_status_code = gr.Textbox(label="Status code from OpenAI server")
28
-
29
- with gr.Accordion("Parameters", open=False):
30
- top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)")
31
- temperature = gr.Slider(minimum=0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature")
32
- chat_counter = gr.Number(value=0, visible=False, precision=0)
33
-
34
- inputs.submit(reset_textbox, [], [inputs, b1], queue=False)
35
- inputs.submit(predict, [inputs, top_p, temperature, chat_counter, chatbot, state],
36
- [chatbot, state, chat_counter, server_status_code, inputs, b1])
37
- b1.click(reset_textbox, [], [inputs, b1], queue=False)
38
- b1.click(predict, [inputs, top_p, temperature, chat_counter, chatbot, state],
39
- [chatbot, state, chat_counter, server_status_code, inputs, b1])
40
-
41
- 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"))
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": False,
27
+ "presence_penalty": 0,
28
+ "frequency_penalty": 0,
29
+ }
30
+ OPENAI_API_KEY = random.choice(OPENAI_API_KEYS)
31
+ headers = {
32
+ "Content-Type": "application/json",
33
+ "Authorization": f"Bearer {OPENAI_API_KEY}",
34
+ }
35
+
36
+ try:
37
+ response = requests.post(API_URL, headers=headers, json=payload)
38
+ response.raise_for_status()
39
+ data = response.json()
40
+
41
+ reply = data['choices'][0]['message']['content']
42
+
43
+ history.append(inputs)
44
+ history.append(reply)
45
+ chat_counter += 1
46
+
47
+ chatbot = [(history[i], history[i+1]) for i in range(0, len(history) - 1, 2)]
48
+ return chatbot, history, chat_counter, str(response.status_code), gr.update(interactive=False), gr.update(interactive=False)
49
+
50
+ except Exception as e:
51
+ print(f"Error: {e}")
52
+ return chatbot, history, chat_counter, f"Error: {e}", gr.update(interactive=True), gr.update(interactive=True)
53
+
54
+ def reset_textbox():
55
+ return gr.update(value='', interactive=False), gr.update(interactive=False)
56
+
57
+ title = """<h1 align="center">GPT-4.1 mini: Research Preview (Short-Term Availability)</h1>"""
58
+ if DISABLED:
59
+ title = """<h1 align="center" style="color:red">This app has reached OpenAI's usage limit. Please check back tomorrow.</h1>"""
60
+ description = """Language models can be conditioned to act like dialogue agents through a conversational prompt that typically takes the form: