Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,6 @@ import random
|
|
| 11 |
|
| 12 |
MODEL = "gpt-4o-mini"
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
API_URL = os.getenv("API_URL")
|
| 17 |
if not API_URL:
|
| 18 |
raise RuntimeError("API_URL not set in Hugging Face Secrets")
|
|
@@ -29,7 +27,6 @@ if not OPENAI_API_KEYS:
|
|
| 29 |
|
| 30 |
NUM_THREADS = int(os.getenv("NUM_THREADS", "2"))
|
| 31 |
|
| 32 |
-
# Safe debug logs (do NOT print actual keys)
|
| 33 |
print("HF Space started")
|
| 34 |
print("API_URL loaded")
|
| 35 |
print("OPENAI_API_KEYS count:", len(OPENAI_API_KEYS))
|
|
@@ -46,7 +43,7 @@ sys.excepthook = exception_handler
|
|
| 46 |
sys.tracebacklimit = 0
|
| 47 |
|
| 48 |
# =====================================================
|
| 49 |
-
# Prediction function
|
| 50 |
# =====================================================
|
| 51 |
|
| 52 |
def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request: gr.Request):
|
|
@@ -55,91 +52,54 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
| 55 |
|
| 56 |
OPENAI_API_KEY = random.choice(OPENAI_API_KEYS)
|
| 57 |
|
| 58 |
-
headers_dict = {
|
| 59 |
-
key.decode("utf-8"): value.decode("utf-8")
|
| 60 |
-
for key, value in request.headers.raw
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
headers = {
|
| 64 |
"Content-Type": "application/json",
|
| 65 |
-
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
| 66 |
-
"Headers": json.dumps(headers_dict),
|
| 67 |
}
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
if chat_counter != 0:
|
| 72 |
-
for i, data in enumerate(history):
|
| 73 |
-
role = "user" if i % 2 == 0 else "assistant"
|
| 74 |
-
messages.append({"role": role, "content": data})
|
| 75 |
-
|
| 76 |
-
messages.append({"role": "user", "content": inputs})
|
| 77 |
-
|
| 78 |
payload = {
|
| 79 |
"model": MODEL,
|
| 80 |
-
"
|
| 81 |
"temperature": temperature,
|
| 82 |
-
"top_p": top_p
|
| 83 |
-
"n": 1,
|
| 84 |
-
"stream": True,
|
| 85 |
-
"presence_penalty": 0,
|
| 86 |
-
"frequency_penalty": 0,
|
| 87 |
}
|
| 88 |
|
| 89 |
chat_counter += 1
|
| 90 |
history.append(inputs)
|
| 91 |
|
| 92 |
-
partial_words = ""
|
| 93 |
-
token_counter = 0
|
| 94 |
-
counter = 0
|
| 95 |
-
|
| 96 |
try:
|
| 97 |
-
response = requests.post(
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
)
|
| 104 |
|
| 105 |
-
for chunk in response.iter_lines():
|
| 106 |
-
if counter == 0:
|
| 107 |
-
counter += 1
|
| 108 |
-
continue
|
| 109 |
-
|
| 110 |
-
if chunk:
|
| 111 |
-
decoded = chunk.decode()
|
| 112 |
-
if len(decoded) > 12:
|
| 113 |
-
data = json.loads(decoded[6:])
|
| 114 |
-
delta = data["choices"][0]["delta"]
|
| 115 |
-
if "content" in delta:
|
| 116 |
-
partial_words += delta["content"]
|
| 117 |
-
if token_counter == 0:
|
| 118 |
-
history.append(" " + partial_words)
|
| 119 |
-
else:
|
| 120 |
-
history[-1] = partial_words
|
| 121 |
-
token_counter += 1
|
| 122 |
-
|
| 123 |
-
yield (
|
| 124 |
-
[(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)],
|
| 125 |
-
history,
|
| 126 |
-
chat_counter,
|
| 127 |
-
response,
|
| 128 |
-
gr.update(interactive=False),
|
| 129 |
-
gr.update(interactive=False),
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
except Exception as e:
|
| 133 |
-
print(f"
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
)
|
| 143 |
|
| 144 |
# =====================================================
|
| 145 |
# UI helpers
|
|
@@ -152,15 +112,13 @@ def reset_textbox():
|
|
| 152 |
# Gradio UI
|
| 153 |
# =====================================================
|
| 154 |
|
| 155 |
-
title = "
|
| 156 |
if DISABLED:
|
| 157 |
title = """<h1 align="center" style="color:red">
|
| 158 |
This app has reached its usage limit. Please check back later.
|
| 159 |
</h1>"""
|
| 160 |
|
| 161 |
-
description = ""
|
| 162 |
-
kutti.
|
| 163 |
-
"""
|
| 164 |
|
| 165 |
theme = gr.themes.Default(primary_hue="green")
|
| 166 |
|
|
|
|
| 11 |
|
| 12 |
MODEL = "gpt-4o-mini"
|
| 13 |
|
|
|
|
|
|
|
| 14 |
API_URL = os.getenv("API_URL")
|
| 15 |
if not API_URL:
|
| 16 |
raise RuntimeError("API_URL not set in Hugging Face Secrets")
|
|
|
|
| 27 |
|
| 28 |
NUM_THREADS = int(os.getenv("NUM_THREADS", "2"))
|
| 29 |
|
|
|
|
| 30 |
print("HF Space started")
|
| 31 |
print("API_URL loaded")
|
| 32 |
print("OPENAI_API_KEYS count:", len(OPENAI_API_KEYS))
|
|
|
|
| 43 |
sys.tracebacklimit = 0
|
| 44 |
|
| 45 |
# =====================================================
|
| 46 |
+
# Prediction function (Responses API compatible)
|
| 47 |
# =====================================================
|
| 48 |
|
| 49 |
def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request: gr.Request):
|
|
|
|
| 52 |
|
| 53 |
OPENAI_API_KEY = random.choice(OPENAI_API_KEYS)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
headers = {
|
| 56 |
"Content-Type": "application/json",
|
| 57 |
+
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
|
|
|
| 58 |
}
|
| 59 |
|
| 60 |
+
# Create payload for Responses API
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
payload = {
|
| 62 |
"model": MODEL,
|
| 63 |
+
"input": inputs,
|
| 64 |
"temperature": temperature,
|
| 65 |
+
"top_p": top_p
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
chat_counter += 1
|
| 69 |
history.append(inputs)
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
try:
|
| 72 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
| 73 |
+
print("Status code:", response.status_code)
|
| 74 |
+
|
| 75 |
+
if response.status_code != 200:
|
| 76 |
+
print("Response text:", response.text)
|
| 77 |
+
output_text = f"Error: {response.status_code}"
|
| 78 |
+
else:
|
| 79 |
+
data = response.json()
|
| 80 |
+
# Extract text output from Responses API format
|
| 81 |
+
output_text = data["output"][0]["content"][0]["text"]
|
| 82 |
+
history.append(output_text)
|
| 83 |
+
|
| 84 |
+
yield (
|
| 85 |
+
[(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)],
|
| 86 |
+
history,
|
| 87 |
+
chat_counter,
|
| 88 |
+
response,
|
| 89 |
+
gr.update(interactive=True),
|
| 90 |
+
gr.update(interactive=True),
|
| 91 |
)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
+
print(f"API request error: {e}")
|
| 95 |
+
yield (
|
| 96 |
+
[(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)],
|
| 97 |
+
history,
|
| 98 |
+
chat_counter,
|
| 99 |
+
None,
|
| 100 |
+
gr.update(interactive=True),
|
| 101 |
+
gr.update(interactive=True),
|
| 102 |
+
)
|
|
|
|
| 103 |
|
| 104 |
# =====================================================
|
| 105 |
# UI helpers
|
|
|
|
| 112 |
# Gradio UI
|
| 113 |
# =====================================================
|
| 114 |
|
| 115 |
+
title = "<h1 align='center'>Hello welcome</h1>"
|
| 116 |
if DISABLED:
|
| 117 |
title = """<h1 align="center" style="color:red">
|
| 118 |
This app has reached its usage limit. Please check back later.
|
| 119 |
</h1>"""
|
| 120 |
|
| 121 |
+
description = "kutti."
|
|
|
|
|
|
|
| 122 |
|
| 123 |
theme = gr.themes.Default(primary_hue="green")
|
| 124 |
|