Spaces:
Sleeping
Sleeping
| import os | |
| import logging | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format="%(asctime)s | %(levelname)s | %(message)s", | |
| datefmt="%Y-%m-%d %H:%M:%S", | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # Environment variables for configuration | |
| HF_TOKEN = os.environ.get("HF_TOKEN", "") | |
| MODEL_ID = os.environ.get("MODEL_ID", "Qwen/Qwen2.5-72B-Instruct") | |
| DEFAULT_MAX_TOKENS = int(os.environ.get("DEFAULT_MAX_TOKENS", "100")) | |
| DEFAULT_TEMPERATURE = float(os.environ.get("DEFAULT_TEMPERATURE", "0.7")) | |
| logger.info(f"HF_TOKEN configured: {bool(HF_TOKEN)}") | |
| logger.info(f"MODEL_ID: {MODEL_ID}") | |
| logger.info(f"DEFAULT_MAX_TOKENS: {DEFAULT_MAX_TOKENS}") | |
| logger.info(f"DEFAULT_TEMPERATURE: {DEFAULT_TEMPERATURE}") | |
| client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else InferenceClient() | |
| logger.info("InferenceClient initialized") | |
| def generate(prompt: str, max_tokens: int, temperature: float) -> str: | |
| """Generate text from prompt.""" | |
| logger.info(f"generate() called | prompt_len={len(prompt)} | max_tokens={max_tokens} | temp={temperature}") | |
| if not prompt.strip(): | |
| logger.warning("Empty prompt received") | |
| return "✏️ Enter a prompt!" | |
| try: | |
| logger.info(f"Calling chat.completions | model={MODEL_ID}") | |
| response = client.chat.completions.create( | |
| model=MODEL_ID, | |
| messages=[{"role": "user", "content": prompt}], | |
| max_tokens=max_tokens, | |
| temperature=temperature, | |
| ) | |
| output = response.choices[0].message.content | |
| logger.info(f"Generated {len(output)} chars") | |
| return output | |
| except Exception as e: | |
| logger.error(f"API error: {e}") | |
| return f"❌ Error: {e}" | |
| logger.info("Building Gradio interface...") | |
| with gr.Blocks(title="Text Playground") as demo: | |
| gr.Markdown("# ✍️ Text Playground\nGenerate creative text with powerful language models!") | |
| prompt = gr.Textbox( | |
| label="Your prompt", | |
| placeholder="Write a haiku about coding...", | |
| lines=3, | |
| autofocus=True, | |
| ) | |
| with gr.Row(equal_height=True): | |
| max_tokens = gr.Slider( | |
| minimum=10, maximum=500, value=DEFAULT_MAX_TOKENS, step=10, | |
| label="Max tokens" | |
| ) | |
| temperature = gr.Slider( | |
| minimum=0.1, maximum=1.5, value=DEFAULT_TEMPERATURE, step=0.1, | |
| label="Temperature (creativity)" | |
| ) | |
| output = gr.Textbox(label="Generated text", lines=6, interactive=False) | |
| btn = gr.Button("Generate ✨", variant="primary") | |
| btn.click(generate, inputs=[prompt, max_tokens, temperature], outputs=output) | |
| prompt.submit(generate, inputs=[prompt, max_tokens, temperature], outputs=output) | |
| gr.Examples( | |
| examples=[ | |
| ["Write a haiku about machine learning", 50, 0.8], | |
| ["Explain quantum computing to a 5-year-old", 150, 0.5], | |
| ["Give me 3 creative startup ideas", 150, 0.9], | |
| ], | |
| inputs=[prompt, max_tokens, temperature], | |
| ) | |
| demo.queue() | |
| logger.info("Starting Gradio server...") | |
| demo.launch() | |