Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- README.md +7 -5
- app.py +80 -0
- requirements.txt +2 -0
README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: HealthBenchDemo
|
| 3 |
+
emoji: 🔥
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.42.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
+
|
app.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# Read your key from env (set this in HF Spaces -> Settings -> Secrets)
|
| 6 |
+
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
if not OPENAI_API_KEY:
|
| 8 |
+
raise RuntimeError("Missing OPENAI_API_KEY environment variable.")
|
| 9 |
+
|
| 10 |
+
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 11 |
+
|
| 12 |
+
DEFAULT_SYS_PROMPT = "You are a helpful assistant. Be concise and accurate."
|
| 13 |
+
|
| 14 |
+
def chat_fn(message, history, system_prompt, temperature, model_name):
|
| 15 |
+
"""
|
| 16 |
+
message: latest user text (str)
|
| 17 |
+
history: list[dict] like [{'role':'user'|'assistant','content': '...'}, ...]
|
| 18 |
+
system_prompt: system instructions (str)
|
| 19 |
+
temperature: float
|
| 20 |
+
model_name: str (e.g., 'gpt-4o-mini' or 'gpt-5')
|
| 21 |
+
"""
|
| 22 |
+
# Build messages for the Chat Completions API
|
| 23 |
+
messages = []
|
| 24 |
+
if system_prompt and system_prompt.strip():
|
| 25 |
+
messages.append({"role": "system", "content": system_prompt.strip()})
|
| 26 |
+
# Append prior turns
|
| 27 |
+
messages.extend(history or [])
|
| 28 |
+
# Add the latest user turn
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
# Stream tokens and yield partials for Gradio
|
| 32 |
+
completion = client.chat.completions.create(
|
| 33 |
+
model=model_name,
|
| 34 |
+
messages=messages,
|
| 35 |
+
temperature=float(temperature),
|
| 36 |
+
stream=True,
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
partial = ""
|
| 40 |
+
for chunk in completion:
|
| 41 |
+
delta = chunk.choices[0].delta
|
| 42 |
+
if delta and delta.content:
|
| 43 |
+
partial += delta.content
|
| 44 |
+
yield partial
|
| 45 |
+
|
| 46 |
+
with gr.Blocks(title="Healthelic") as demo:
|
| 47 |
+
gr.Markdown("## Healthelic Chat\nA lightweight, streaming chat app.")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
system_prompt = gr.Textbox(
|
| 51 |
+
value=DEFAULT_SYS_PROMPT,
|
| 52 |
+
label="System prompt",
|
| 53 |
+
lines=2
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
model_name = gr.Dropdown(
|
| 58 |
+
choices=["gpt-4o-mini", "gpt-4o", "gpt-5"],
|
| 59 |
+
value="gpt-5",
|
| 60 |
+
label="Model"
|
| 61 |
+
)
|
| 62 |
+
temperature = gr.Slider(0.0, 1.2, value=0.7, step=0.1, label="Temperature")
|
| 63 |
+
|
| 64 |
+
chat = gr.ChatInterface(
|
| 65 |
+
fn=chat_fn,
|
| 66 |
+
type="messages",
|
| 67 |
+
additional_inputs=[system_prompt, temperature, model_name],
|
| 68 |
+
textbox=gr.Textbox(placeholder="Ask me anything…"),
|
| 69 |
+
examples=[
|
| 70 |
+
["Explain transformers in one paragraph", DEFAULT_SYS_PROMPT, 0.7, "gpt-4o-mini"],
|
| 71 |
+
["Write a dad joke about databases", DEFAULT_SYS_PROMPT, 0.7, "gpt-4o-mini"]
|
| 72 |
+
],
|
| 73 |
+
cache_examples=False,
|
| 74 |
+
save_history=True,
|
| 75 |
+
flagging_mode="manual",
|
| 76 |
+
flagging_options=["👍 Useful", "👎 Not good", "⚠ Inaccurate"],
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
demo.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.44.1
|
| 2 |
+
openai>=1.40.0
|