Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
|
|
@@ -5,16 +6,11 @@ DEFAULT_SYSTEM_PROMPT = "You are a helpful, concise assistant."
|
|
| 5 |
DEFAULT_MODEL = "gpt-5-chat-latest"
|
| 6 |
|
| 7 |
def chat_fn(message, history, system_prompt, temperature, max_tokens, api_key, base_url, model):
|
| 8 |
-
"""
|
| 9 |
-
Gradio passes (message, history, *additional_inputs).
|
| 10 |
-
"""
|
| 11 |
if not api_key:
|
| 12 |
return "⚠️ Please provide an OpenAI API key (top of the page)."
|
| 13 |
|
| 14 |
-
# Build client with the provided key & base URL
|
| 15 |
client = OpenAI(api_key=api_key, base_url=(base_url or "https://api.openai.com/v1").strip())
|
| 16 |
|
| 17 |
-
# Build messages
|
| 18 |
messages = [{"role": "system", "content": (system_prompt or DEFAULT_SYSTEM_PROMPT).strip()}]
|
| 19 |
for user_msg, assistant_msg in history:
|
| 20 |
if user_msg:
|
|
@@ -32,7 +28,6 @@ def chat_fn(message, history, system_prompt, temperature, max_tokens, api_key, b
|
|
| 32 |
)
|
| 33 |
return resp.choices[0].message.content
|
| 34 |
except Exception as e:
|
| 35 |
-
# Surface API errors in the UI for easy debugging
|
| 36 |
return f"❌ Error: {e}"
|
| 37 |
|
| 38 |
with gr.Blocks(title="OpenAI Chat (Gradio)") as demo:
|
|
@@ -53,16 +48,11 @@ with gr.Blocks(title="OpenAI Chat (Gradio)") as demo:
|
|
| 53 |
|
| 54 |
gr.Markdown("---")
|
| 55 |
|
| 56 |
-
|
| 57 |
fn=chat_fn,
|
| 58 |
title="OpenAI Chatbot",
|
| 59 |
-
retry_btn="Retry",
|
| 60 |
-
undo_btn="Remove last",
|
| 61 |
-
clear_btn="Clear",
|
| 62 |
-
submit_btn="Send",
|
| 63 |
additional_inputs=[system_prompt, temperature, max_tokens, api_key, base_url, model],
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
# Set share=True if you need a public URL while testing
|
| 68 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
|
|
|
|
| 6 |
DEFAULT_MODEL = "gpt-5-chat-latest"
|
| 7 |
|
| 8 |
def chat_fn(message, history, system_prompt, temperature, max_tokens, api_key, base_url, model):
|
|
|
|
|
|
|
|
|
|
| 9 |
if not api_key:
|
| 10 |
return "⚠️ Please provide an OpenAI API key (top of the page)."
|
| 11 |
|
|
|
|
| 12 |
client = OpenAI(api_key=api_key, base_url=(base_url or "https://api.openai.com/v1").strip())
|
| 13 |
|
|
|
|
| 14 |
messages = [{"role": "system", "content": (system_prompt or DEFAULT_SYSTEM_PROMPT).strip()}]
|
| 15 |
for user_msg, assistant_msg in history:
|
| 16 |
if user_msg:
|
|
|
|
| 28 |
)
|
| 29 |
return resp.choices[0].message.content
|
| 30 |
except Exception as e:
|
|
|
|
| 31 |
return f"❌ Error: {e}"
|
| 32 |
|
| 33 |
with gr.Blocks(title="OpenAI Chat (Gradio)") as demo:
|
|
|
|
| 48 |
|
| 49 |
gr.Markdown("---")
|
| 50 |
|
| 51 |
+
gr.ChatInterface(
|
| 52 |
fn=chat_fn,
|
| 53 |
title="OpenAI Chatbot",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
additional_inputs=[system_prompt, temperature, max_tokens, api_key, base_url, model],
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
|
|
|
| 58 |
demo.launch()
|