kasper-boy
commited on
Commit
β’
84ee036
1
Parent(s):
7b215b6
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,33 @@
|
|
|
|
1 |
import torch
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
from diffusers import StableDiffusionPipeline
|
5 |
-
|
6 |
-
# Use a pipeline as a high-level helper
|
7 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
|
11 |
caption_image = pipeline("image-to-text",
|
12 |
model="Salesforce/blip-image-captioning-large", device=device)
|
13 |
|
14 |
-
|
15 |
def image_generation(prompt):
|
16 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
17 |
pipeline = StableDiffusionPipeline.from_pretrained(
|
18 |
"stabilityai/stable-diffusion-3-medium",
|
19 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
|
|
20 |
)
|
21 |
-
#pipeline.to(device)
|
22 |
pipeline.enable_model_cpu_offload()
|
23 |
-
|
24 |
image = pipeline(
|
25 |
prompt=prompt + " 8K, Ultra HD",
|
26 |
negative_prompt="blurred, ugly, watermark, low resolution, blurry, nude",
|
@@ -38,8 +45,8 @@ def caption_my_image(pil_image):
|
|
38 |
return images
|
39 |
|
40 |
demo = gr.Interface(fn=caption_my_image,
|
41 |
-
inputs=[gr.Image(label="Select Image",type="pil")],
|
42 |
-
outputs=[gr.Image(label="New Image
|
43 |
title="PicTalker | ImageNarrator | SnapSpeech | SpeakScene",
|
44 |
description="π Transform Ordinary Photos into Extraordinary Art!")
|
45 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import torch
|
3 |
import gradio as gr
|
4 |
from PIL import Image
|
5 |
from diffusers import StableDiffusionPipeline
|
|
|
|
|
6 |
from transformers import pipeline
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
|
9 |
+
# Load environment variables from .env file
|
10 |
+
load_dotenv()
|
11 |
+
|
12 |
+
# Set Hugging Face API token from environment variable
|
13 |
+
hf_token = os.getenv('HF_TOKEN')
|
14 |
+
if not hf_token:
|
15 |
+
raise ValueError("Hugging Face API token not found. Please set HF_TOKEN in your .env file.")
|
16 |
|
17 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
|
19 |
caption_image = pipeline("image-to-text",
|
20 |
model="Salesforce/blip-image-captioning-large", device=device)
|
21 |
|
|
|
22 |
def image_generation(prompt):
|
23 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
pipeline = StableDiffusionPipeline.from_pretrained(
|
25 |
"stabilityai/stable-diffusion-3-medium",
|
26 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
27 |
+
use_auth_token=hf_token # Use the Hugging Face API token for authentication
|
28 |
)
|
|
|
29 |
pipeline.enable_model_cpu_offload()
|
30 |
+
|
31 |
image = pipeline(
|
32 |
prompt=prompt + " 8K, Ultra HD",
|
33 |
negative_prompt="blurred, ugly, watermark, low resolution, blurry, nude",
|
|
|
45 |
return images
|
46 |
|
47 |
demo = gr.Interface(fn=caption_my_image,
|
48 |
+
inputs=[gr.Image(label="Select Image", type="pil")],
|
49 |
+
outputs=[gr.Image(label="New Image generated using SD3", type="pil")],
|
50 |
title="PicTalker | ImageNarrator | SnapSpeech | SpeakScene",
|
51 |
description="π Transform Ordinary Photos into Extraordinary Art!")
|
52 |
+
demo.launch()
|