Download app.py from KevinDiez/Chatbot: direct link, hf CLI and curl.
- Browser
- Download file 2.7 kB
-
https://huggingface.co/spaces/KevinDiez/Chatbot/resolve/main/app.py
- Command line
-
hf download hf://spaces/KevinDiez/Chatbot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/KevinDiez/Chatbot/resolve/main/app.py
2.7 kB
| import gradio as gr | |
| import requests | |
| from io import BytesIO | |
| from PIL import Image | |
| HUGGINGFACE_API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2" | |
| HUGGINGFACE_API_TOKEN = "your_huggingface_api_token_here" # Replace with your actual token | |
| headers = { | |
| "Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}" | |
| } | |
| def text_to_image(prompt): | |
| if not prompt.strip(): | |
| return None | |
| payload = { | |
| "inputs": prompt, | |
| } | |
| response = requests.post(HUGGINGFACE_API_URL, headers=headers, json=payload) | |
| if response.status_code != 200: | |
| return None | |
| image_bytes = BytesIO(response.content) | |
| image = Image.open(image_bytes).convert("RGB") | |
| return image | |
| css = """ | |
| body { | |
| background-color: #ffffff; | |
| font-family: 'Inter', sans-serif; | |
| margin: 0; padding: 0; | |
| } | |
| #container { | |
| max-width: 900px; | |
| margin: 3rem auto; | |
| padding: 1rem; | |
| box-sizing: border-box; | |
| } | |
| #header { | |
| font-weight: 700; | |
| font-size: 48px; | |
| color: #111827; | |
| margin-bottom: 0.5rem; | |
| } | |
| #subheader { | |
| color: #6b7280; | |
| font-size: 18px; | |
| margin-bottom: 2rem; | |
| } | |
| #prompt-input textarea { | |
| font-size: 16px; | |
| padding: 0.75rem; | |
| border-radius: 12px; | |
| border: 1px solid #d1d5db; | |
| resize: vertical; | |
| } | |
| #generate-btn { | |
| background-color: #111827; | |
| color: white; | |
| border: none; | |
| padding: 0.75rem 1.5rem; | |
| font-weight: 600; | |
| border-radius: 12px; | |
| cursor: pointer; | |
| margin-top: 1rem; | |
| transition: background-color 0.3s ease; | |
| } | |
| #generate-btn:hover { | |
| background-color: #374151; | |
| } | |
| #output-image { | |
| margin-top: 2rem; | |
| border-radius: 12px; | |
| box-shadow: 0 4px 12px rgba(0,0,0,0.05); | |
| max-width: 100%; | |
| } | |
| """ | |
| with gr.Blocks(css=css) as demo: | |
| with gr.Row(): | |
| with gr.Column(elem_id="container"): | |
| gr.Markdown("<h1 id='header'>🖼️ Text to Visual Image Chatbot</h1>") | |
| gr.Markdown("<p id='subheader'>Enter a descriptive text prompt and get a generated image.</p>") | |
| prompt_input = gr.Textbox( | |
| label="Your text prompt", | |
| placeholder="Describe the image you want to generate...", | |
| lines=3, | |
| elem_id="prompt-input" | |
| ) | |
| generate_btn = gr.Button("Generate Image", elem_id="generate-btn") | |
| output_image = gr.Image(type="pil", elem_id="output-image") | |
| def on_generate(prompt): | |
| img = text_to_image(prompt) | |
| if img is None: | |
| return None | |
| return img | |
| generate_btn.click(on_generate, inputs=prompt_input, outputs=output_image) | |
| if __name__ == "__main__": | |
| demo.launch() | |