Spaces:
Build error
Build error
Download app.py from E66andF10w/AIConstructing: direct link, hf CLI and curl.
- Browser
- Download file 1.3 kB
-
https://huggingface.co/spaces/E66andF10w/AIConstructing/resolve/main/app.py
- Command line
-
hf download hf://spaces/E66andF10w/AIConstructing/app.py
-
curl -L -o app.py https://huggingface.co/spaces/E66andF10w/AIConstructing/resolve/main/app.py
1.3 kB
| import gradio as gr | |
| from transformers import GPT2LMHeadModel, GPT2Tokenizer | |
| from diffusers import StableDiffusionPipeline | |
| import torch | |
| # ε θ½½ GPT-2 樑εεεθ―ε¨ | |
| model_name = "gpt2" | |
| tokenizer = GPT2Tokenizer.from_pretrained(model_name) | |
| model = GPT2LMHeadModel.from_pretrained(model_name) | |
| # ε θ½½ Stable Diffusion 樑ε | |
| pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4") | |
| pipe.to("cpu") | |
| # ζζ¬ηζε½ζ° | |
| def generate_text(prompt): | |
| inputs = tokenizer.encode(prompt, return_tensors="pt") | |
| outputs = model.generate(inputs, max_length=50, num_return_sequences=1) | |
| return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # εΎεηζε½ζ° | |
| def generate_image_from_text(prompt): | |
| image = pipe(prompt).images[0] | |
| return image | |
| # ζζ¬ηζ -> εΎεηζ | |
| def process(prompt): | |
| generated_text = generate_text(prompt) | |
| image = generate_image_from_text(generated_text) | |
| return generated_text, image | |
| # εε»Ί Gradio ζ₯ε£ | |
| iface = gr.Interface( | |
| fn=process, | |
| inputs=gr.Textbox(label="θΎε ₯ζζ¬"), | |
| outputs=[gr.Textbox(label="ηζζζ¬"), gr.Image(label="ηζεΎε")], | |
| title="ζζ¬ηζεΎε", | |
| description="θΎε ₯ζζ¬οΌηζηΈεΊηεΎεγ", | |
| ) | |
| # ε―ε¨ Gradio εΊη¨ | |
| iface.launch(share=True) |