Spaces:
Sleeping
Sleeping
shreeramy
commited on
Commit
·
414c785
1
Parent(s):
bc6b153
Add application file
Browse files- app.py +82 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from typing import Optional
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Load Hugging Face Token from environment variables
|
| 10 |
+
def load_environment():
|
| 11 |
+
return os.getenv("HF_TOKEN")
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Enhance the text prompt for better photorealistic results
|
| 15 |
+
def craft_realistic_prompt(base_prompt: str) -> str:
|
| 16 |
+
realistic_modifiers = [
|
| 17 |
+
"photorealistic", "high resolution", "sharp focus",
|
| 18 |
+
"professional photography", "natural lighting", "detailed textures"
|
| 19 |
+
]
|
| 20 |
+
return f"{' '.join(realistic_modifiers)}, {base_prompt}, shot on professional camera, 8k resolution"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
# Query Hugging Face API to generate image from text
|
| 24 |
+
def query_hf_api(prompt: str) -> Optional[bytes]:
|
| 25 |
+
model_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0"
|
| 26 |
+
HF_TOKEN = load_environment()
|
| 27 |
+
|
| 28 |
+
if not HF_TOKEN:
|
| 29 |
+
raise ValueError("Hugging Face token not found. Set HF_TOKEN in environment variables.")
|
| 30 |
+
|
| 31 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}", "Content-Type": "application/json"}
|
| 32 |
+
payload = {
|
| 33 |
+
"inputs": craft_realistic_prompt(prompt),
|
| 34 |
+
"parameters": {
|
| 35 |
+
"negative_prompt": "cartoon, anime, low quality, bad anatomy, blurry, unrealistic, painting, drawing, sketch",
|
| 36 |
+
"num_inference_steps": 75,
|
| 37 |
+
"guidance_scale": 8.5,
|
| 38 |
+
}
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
response = requests.post(model_url, headers=headers, json=payload, timeout=120)
|
| 42 |
+
response.raise_for_status()
|
| 43 |
+
return response.content
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Generate image from text and return as PIL image
|
| 47 |
+
def generate_image(prompt: str):
|
| 48 |
+
try:
|
| 49 |
+
image_bytes = query_hf_api(prompt)
|
| 50 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 51 |
+
return image
|
| 52 |
+
except Exception as e:
|
| 53 |
+
return f"Error: {str(e)}"
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Modern Gradio UI using Blocks
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("# 🎨 Wangoes Text-to-Image Generator")
|
| 59 |
+
gr.Markdown("Generate stunning AI images from text using Hugging Face models.")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
with gr.Column():
|
| 63 |
+
prompt_input = gr.Textbox(label="Enter your prompt",
|
| 64 |
+
placeholder="Describe the image you want to generate...")
|
| 65 |
+
generate_button = gr.Button("Generate Image", variant="primary")
|
| 66 |
+
|
| 67 |
+
with gr.Column():
|
| 68 |
+
image_output = gr.Image(label="Generated Image", type="pil")
|
| 69 |
+
|
| 70 |
+
gr.Examples(
|
| 71 |
+
examples=[
|
| 72 |
+
"A futuristic cityscape at sunset with flying cars",
|
| 73 |
+
"A serene mountain lake with crystal clear water",
|
| 74 |
+
"A cozy cabin in the woods during winter"
|
| 75 |
+
],
|
| 76 |
+
inputs=prompt_input
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
generate_button.click(fn=generate_image, inputs=prompt_input, outputs=image_output)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
Pillow
|
| 4 |
+
python-dotenv
|