Huzaifa367 commited on
Commit
dbb04fd
·
verified ·
1 Parent(s): 4c16c97

Create textimage.py

Browse files
Files changed (1) hide show
  1. pages/textimage.py +69 -0
pages/textimage.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import random
4
+ import spaces
5
+ import numpy as np
6
+ import torch
7
+ from typing import Tuple
8
+ from datetime import datetime
9
+ from diffusers import PixArtAlphaPipeline, LCMScheduler
10
+
11
+ # Check if CUDA is available
12
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
13
+
14
+ # Define Hugging Face API details
15
+ API_URL = "https://api-inference.huggingface.co/models/Huzaifa367/chat-summarizer"
16
+ API_TOKEN = os.getenv("AUTH_TOKEN")
17
+ HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
18
+
19
+ # Initialize PixArtAlphaPipeline
20
+ pipe = PixArtAlphaPipeline.from_pretrained(
21
+ "PixArt-alpha/PixArt-LCM-XL-2-1024-MS",
22
+ torch_dtype=torch.float16,
23
+ use_safetensors=True,
24
+ device=device
25
+ )
26
+
27
+ # Function to generate image based on prompt
28
+ def generate_image(prompt: str) -> Tuple[str, int]:
29
+ seed = random.randint(0, np.iinfo(np.int32).max)
30
+ images = pipe(
31
+ prompt=prompt,
32
+ width=1024,
33
+ height=1024,
34
+ num_inference_steps=4,
35
+ generator=torch.Generator().manual_seed(seed),
36
+ num_images_per_prompt=1,
37
+ use_resolution_binning=True,
38
+ output_type="pil",
39
+ ).images
40
+
41
+ # Save image and return path and seed
42
+ image_path = save_image(images[0])
43
+ return image_path, seed
44
+
45
+ # Function to save image and return path
46
+ def save_image(img):
47
+ unique_name = str(uuid.uuid4()) + ".png"
48
+ img.save(unique_name)
49
+ return unique_name
50
+
51
+ # Streamlit app
52
+ def main():
53
+ st.set_page_config(layout="wide")
54
+ st.title("Instant Image Generator")
55
+
56
+ # Prompt input
57
+ prompt = st.text_area("Prompt", "Enter your prompt here...")
58
+
59
+ # Generate button
60
+ if st.button("Generate Image"):
61
+ if prompt:
62
+ # Generate image based on prompt
63
+ image_path, seed = generate_image(prompt)
64
+
65
+ # Display the generated image
66
+ st.image(image_path, use_column_width=True, caption=f"Seed: {seed}")
67
+
68
+ if __name__ == "__main__":
69
+ main()