Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,7 @@ import random
|
|
| 9 |
# Environment variables (Hugging Face Spaces SAFE)
|
| 10 |
# =====================================================
|
| 11 |
|
| 12 |
-
MODEL = "gpt-
|
| 13 |
|
| 14 |
API_URL = os.getenv("API_URL")
|
| 15 |
if not API_URL:
|
|
@@ -57,7 +57,7 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
| 57 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
| 58 |
}
|
| 59 |
|
| 60 |
-
#
|
| 61 |
payload = {
|
| 62 |
"model": MODEL,
|
| 63 |
"input": inputs,
|
|
@@ -66,7 +66,7 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
| 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)
|
|
@@ -77,29 +77,19 @@ def predict(inputs, top_p, temperature, chat_counter, chatbot, history, request:
|
|
| 77 |
output_text = f"Error: {response.status_code}"
|
| 78 |
else:
|
| 79 |
data = response.json()
|
| 80 |
-
# Extract text output from Responses API
|
| 81 |
output_text = data["output"][0]["content"][0]["text"]
|
| 82 |
-
history.append(output_text)
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 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 |
-
|
| 96 |
-
|
| 97 |
-
history,
|
| 98 |
-
chat_counter,
|
| 99 |
-
None,
|
| 100 |
-
gr.update(interactive=True),
|
| 101 |
-
gr.update(interactive=True),
|
| 102 |
-
)
|
| 103 |
|
| 104 |
# =====================================================
|
| 105 |
# UI helpers
|
|
@@ -134,7 +124,7 @@ with gr.Blocks(
|
|
| 134 |
gr.HTML("<h3 align='center'>I'm Kutti</h3>")
|
| 135 |
|
| 136 |
with gr.Column(elem_id="col_container", visible=False) as main_block:
|
| 137 |
-
chatbot = gr.Chatbot(elem_id="chatbot")
|
| 138 |
inputs = gr.Textbox(placeholder="Hi there!", label="Type an input and press Enter")
|
| 139 |
state = gr.State([])
|
| 140 |
|
|
|
|
| 9 |
# Environment variables (Hugging Face Spaces SAFE)
|
| 10 |
# =====================================================
|
| 11 |
|
| 12 |
+
MODEL = "gpt-4.1-mini" # Updated to a valid model for Responses API
|
| 13 |
|
| 14 |
API_URL = os.getenv("API_URL")
|
| 15 |
if not API_URL:
|
|
|
|
| 57 |
"Authorization": f"Bearer {OPENAI_API_KEY}"
|
| 58 |
}
|
| 59 |
|
| 60 |
+
# Build payload for Responses API
|
| 61 |
payload = {
|
| 62 |
"model": MODEL,
|
| 63 |
"input": inputs,
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
chat_counter += 1
|
| 69 |
+
history.append({"role": "user", "content": inputs})
|
| 70 |
|
| 71 |
try:
|
| 72 |
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
|
|
|
| 77 |
output_text = f"Error: {response.status_code}"
|
| 78 |
else:
|
| 79 |
data = response.json()
|
| 80 |
+
# Extract text output from Responses API
|
| 81 |
output_text = data["output"][0]["content"][0]["text"]
|
| 82 |
+
history.append({"role": "assistant", "content": output_text})
|
| 83 |
|
| 84 |
+
# Convert history to Gradio 'messages' format
|
| 85 |
+
messages = [{"role": msg["role"], "content": msg["content"]} for msg in history]
|
| 86 |
+
|
| 87 |
+
yield messages, history, chat_counter, response, gr.update(interactive=True), gr.update(interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
print(f"API request error: {e}")
|
| 91 |
+
messages = [{"role": msg["role"], "content": msg["content"]} for msg in history]
|
| 92 |
+
yield messages, history, chat_counter, None, gr.update(interactive=True), gr.update(interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
# =====================================================
|
| 95 |
# UI helpers
|
|
|
|
| 124 |
gr.HTML("<h3 align='center'>I'm Kutti</h3>")
|
| 125 |
|
| 126 |
with gr.Column(elem_id="col_container", visible=False) as main_block:
|
| 127 |
+
chatbot = gr.Chatbot(elem_id="chatbot", type="messages") # Fixed warning
|
| 128 |
inputs = gr.Textbox(placeholder="Hi there!", label="Type an input and press Enter")
|
| 129 |
state = gr.State([])
|
| 130 |
|