Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from transformers import pipeline | |
| from diffusers import AutoPipelineForText2Image | |
| import torch | |
| # -------- TEXT MODEL -------- | |
| text_model = pipeline("text-generation", model="distilgpt2") | |
| # -------- IMAGE MODEL -------- | |
| pipe = AutoPipelineForText2Image.from_pretrained( | |
| "stabilityai/sd-turbo", | |
| torch_dtype=torch.float32 | |
| ) | |
| pipe = pipe.to("cpu") | |
| # -------- FUNCTION -------- | |
| def respond(message, history): | |
| user_input = message | |
| if any(word in user_input.lower() for word in ["draw", "image", "generate", "picture"]): | |
| image = pipe( | |
| user_input, | |
| num_inference_steps=1, | |
| guidance_scale=0.0 | |
| ).images[0] | |
| history.append({"role": "user", "content": user_input}) | |
| history.append({"role": "assistant", "content": "πΌοΈ Image generated"}) | |
| return history, image | |
| else: | |
| result = text_model(user_input, max_length=80)[0]["generated_text"] | |
| history.append({"role": "user", "content": user_input}) | |
| history.append({"role": "assistant", "content": result}) | |
| return history, None | |
| # -------- UI -------- | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# AI Chat + Image (Gradio 6 Fixed)") | |
| chatbot = gr.Chatbot(type="messages") | |
| image_output = gr.Image() | |
| msg = gr.Textbox(placeholder="Ask or generate image...") | |
| state = gr.State([]) | |
| msg.submit(respond, [msg, state], [chatbot, image_output]) | |
| demo.launch() |