Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,60 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|