Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +18 -5
- app.py +63 -0
- requirements.txt +3 -0
README.md
CHANGED
|
@@ -1,12 +1,25 @@
|
|
| 1 |
---
|
| 2 |
title: Text Playground
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 6.0.2
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Text Playground
|
| 3 |
+
emoji: ✍️
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: "6.0.2"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
## ✍️ Text Playground
|
| 14 |
+
|
| 15 |
+
Generate creative text with Zephyr-7B via the HuggingFace Inference API.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- Adjustable temperature and max tokens
|
| 20 |
+
- Creative text generation
|
| 21 |
+
- No model downloads - uses API
|
| 22 |
+
|
| 23 |
+
## Setup
|
| 24 |
+
|
| 25 |
+
Add your `HF_TOKEN` as a Secret in Space Settings.
|
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Get token from environment (set in HF Space secrets)
|
| 6 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 7 |
+
client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else InferenceClient()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def generate(prompt: str, max_tokens: int, temperature: float) -> str:
|
| 11 |
+
"""Generate text from prompt."""
|
| 12 |
+
if not prompt.strip():
|
| 13 |
+
return "✏️ Enter a prompt!"
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
response = client.chat.completions.create(
|
| 17 |
+
model="HuggingFaceH4/zephyr-7b-beta",
|
| 18 |
+
messages=[{"role": "user", "content": prompt}],
|
| 19 |
+
max_tokens=max_tokens,
|
| 20 |
+
temperature=temperature,
|
| 21 |
+
)
|
| 22 |
+
return response.choices[0].message.content
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"❌ Error: {e}"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
with gr.Blocks(title="Text Playground") as demo:
|
| 28 |
+
gr.Markdown("# ✍️ Text Playground\nGenerate creative text with powerful language models!")
|
| 29 |
+
|
| 30 |
+
prompt = gr.Textbox(
|
| 31 |
+
label="Your prompt",
|
| 32 |
+
placeholder="Write a haiku about coding...",
|
| 33 |
+
lines=3,
|
| 34 |
+
autofocus=True,
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
with gr.Row(equal_height=True):
|
| 38 |
+
max_tokens = gr.Slider(
|
| 39 |
+
minimum=10, maximum=200, value=100, step=10,
|
| 40 |
+
label="Max tokens"
|
| 41 |
+
)
|
| 42 |
+
temperature = gr.Slider(
|
| 43 |
+
minimum=0.1, maximum=1.5, value=0.7, step=0.1,
|
| 44 |
+
label="Temperature (creativity)"
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
output = gr.Textbox(label="Generated text", lines=6, interactive=False)
|
| 48 |
+
btn = gr.Button("Generate ✨", variant="primary")
|
| 49 |
+
|
| 50 |
+
btn.click(generate, inputs=[prompt, max_tokens, temperature], outputs=output)
|
| 51 |
+
prompt.submit(generate, inputs=[prompt, max_tokens, temperature], outputs=output)
|
| 52 |
+
|
| 53 |
+
gr.Examples(
|
| 54 |
+
examples=[
|
| 55 |
+
["Write a haiku about machine learning", 50, 0.8],
|
| 56 |
+
["Explain quantum computing to a 5-year-old", 150, 0.5],
|
| 57 |
+
["Give me 3 creative startup ideas", 150, 0.9],
|
| 58 |
+
],
|
| 59 |
+
inputs=[prompt, max_tokens, temperature],
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
demo.queue()
|
| 63 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=6.0.0
|
| 2 |
+
huggingface_hub>=0.23.0
|
| 3 |
+
|