Spaces:
Runtime error
Runtime error
File size: 9,256 Bytes
75d0925 9f48bb2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
import pinecone
import torch
import numpy as np
from PIL import Image
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel, UniPCMultistepScheduler
from datasets import load_dataset
from transformers import pipeline
import gradio as gr
import pinecone
from pinecone import Pinecone
pc = Pinecone(api_key="23afd6c8-4e05-4f77-a069-95ad7b18e6cd")
from datasets import load_dataset
# Load OpenAI CLIP model for embedding generation
import open_clip
model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-32', pretrained='openai')
tokenizer = open_clip.get_tokenizer('ViT-B-32')
depth_estimator = pipeline("depth-estimation")
# Initialize Stable Diffusion ControlNet
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel
from diffusers.utils.import_utils import is_xformers_available
from diffusers.schedulers import UniPCMultistepScheduler
import torch
controlnet = ControlNetModel.from_pretrained("lllyasviel/control_v11f1p_sd15_depth", torch_dtype=torch.float16).to("cuda")
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
).to("cuda")
# Load your fine-tuned LoRA adapter
lora_weights_path = "rohith2812/atoi-lora-finetuned-v1" # Replace with your LoRA weight file path
pipe.unet.load_attn_procs(lora_weights_path)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
if is_xformers_available():
pipe.enable_xformers_memory_efficient_attention()
print("Pipeline is ready with fine-tuned LoRA adapter!")
transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
def retrieve_image_from_text_prompt(prompt, selected_index, knowledge_database):
"""
Retrieve the most relevant image based on the text prompt from the selected Pinecone index.
"""
dataset = load_dataset(knowledge_database, split="train")
# Initialize the Pinecone index dynamically based on user selection
index = pc.Index(selected_index)
# Generate Embedding for Text
text_tokens = tokenizer([prompt])
with torch.no_grad():
query_embedding = model.encode_text(text_tokens).cpu().numpy().flatten()
# Query Pinecone
results = index.query(vector=query_embedding.tolist(), top_k=1, include_metadata=True, namespace="text_embeddings")
if results and "matches" in results and results["matches"]:
best_match = results["matches"][0]
image_path = best_match["metadata"]["image_path"]
description = best_match["metadata"]["description"]
# Match the image path to the dataset to retrieve the image
for item in dataset:
if item["image_path"].endswith(image_path):
return {"image": item["image"], "description": description}
return None
# Function to Generate Depth Map
def get_depth_map(image):
image = depth_estimator(image)["depth"]
image = np.array(image)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
detected_map = torch.from_numpy(image).float() / 255.0
return detected_map.permute(2, 0, 1).unsqueeze(0).half().to("cuda")
from transformers import CLIPProcessor, CLIPModel
import torch
# Load CLIP model and processor
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32").to("cuda")
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
def calculate_clip_score(image, text):
"""Calculate CLIP score for an image and text pair."""
inputs = clip_processor(text=[text], images=image, return_tensors="pt", padding=True).to("cuda")
outputs = clip_model(**inputs)
logits_per_image = outputs.logits_per_image # Image-to-text similarity score
clip_score = logits_per_image.softmax(dim=1).max().item()
return clip_score
def audio_to_image(audio, guidance_scale, num_inference_steps, selected_index, knowledge_database):
# Initialize Pinecone index based on user selection
dataset = load_dataset(knowledge_database, split="train")
index = pc.Index(selected_index)
print(f"Connected to Pinecone index: {selected_index}")
# Step 1: Transcribe Audio
sr, y = audio
if y.ndim > 1:
y = y.mean(axis=1) # Convert to mono
y = y.astype(np.float32)
y /= np.max(np.abs(y))
transcription = transcriber({"sampling_rate": sr, "raw": y})["text"]
print(f"Transcribed Text: {transcription}")
# Step 2: Retrieve Image Based on Text Prompt
print("Retrieving image from vector database...")
retrieved_data = retrieve_image_from_text_prompt(transcription, selected_index, knowledge_database)
if not retrieved_data:
return transcription, None, None, "No relevant image found.", None
retrieved_image = retrieved_data["image"]
retrieved_description = retrieved_data["description"]
# Step 3: Generate Depth Map
print("Generating depth map...")
depth_map = get_depth_map(retrieved_image)
# Step 4: Enhance Image Using Stable Diffusion
print("Enhancing image with Stable Diffusion...")
enhanced_image = pipe(
prompt=f"{transcription}. Ensure formulas are accurate and text is clean and legible.",
image=retrieved_image,
control_image=depth_map,
guidance_scale=guidance_scale,
num_inference_steps=num_inference_steps
).images[0]
# Step 5: Calculate CLIP Score
print("Calculating CLIP Score...")
clip_score = calculate_clip_score(enhanced_image, transcription)
# Return Retrieved and Enhanced Images with CLIP Score
return transcription, retrieved_image, enhanced_image, retrieved_description, clip_score
# Gradio Interface Function
def gradio_interface(audio, guidance_scale, num_inference_steps, selected_index, knowledge_database):
transcription, retrieved_image, enhanced_image, retrieved_description, clip_score = audio_to_image(
audio, guidance_scale, num_inference_steps, selected_index, knowledge_database
)
if enhanced_image is None:
return transcription, "No relevant image found.", None, retrieved_description, "N/A"
return transcription, retrieved_image, enhanced_image, retrieved_description, clip_score
# Enhanced Gradio UI
with gr.Blocks(title="Audio-to-Image Generation") as demo:
gr.Markdown(
"""
# π¨ Audio-to-Image Generation with AI
Speak into the microphone, and watch as this AI application retrieves a relevant image from the database,
enhances it based on your input, and displays its description and CLIP Score.
"""
)
with gr.Row():
with gr.Column():
audio_input = gr.Audio(type="numpy", label="π€ Speak Your Prompt")
guidance_scale_input = gr.Slider(
minimum=1.0, maximum=20.0, step=0.5, value=8.5, label="ποΈ Guidance Scale"
)
num_inference_steps_input = gr.Slider(
minimum=10, maximum=200, step=10, value=100, label="π’ Number of Inference Steps"
)
index_selection = gr.Dropdown(
choices=["project-atoi-v2", "project-atoi"],
value="project-atoi-v2",
label="ποΈ Select Pinecone Index"
)
knowledge_database_selection = gr.Dropdown(
choices=["rohith2812/atoigeneration-final-data", "rxc5667/3wordsdataset_noduplicates"],
value="rxc5667/3wordsdataset_noduplicates",
label="π Select Knowledge Database"
)
submit_button = gr.Button("Generate Image")
with gr.Column():
transcription_output = gr.Textbox(label="π Transcribed Prompt")
retrieved_image_output = gr.Image(label="πΌοΈ Retrieved Image")
enhanced_image_output = gr.Image(label="β¨ Enhanced Image")
retrieved_description_output = gr.Textbox(label="π Retrieved Description")
clip_score_output = gr.Textbox(label="π CLIP Score")
examples = gr.Examples(
examples=[["a picture explain line of best fit in linear regression"], ["Support vector machines"], ["A picture explaining multi[ple components in PCA]"]],
inputs=[
audio_input,
guidance_scale_input,
num_inference_steps_input,
index_selection,
knowledge_database_selection,
],
outputs=[
transcription_output,
retrieved_image_output,
enhanced_image_output,
retrieved_description_output,
clip_score_output,
],
label="Examples",
)
submit_button.click(
fn=gradio_interface,
inputs=[
audio_input,
guidance_scale_input,
num_inference_steps_input,
index_selection,
knowledge_database_selection,
],
outputs=[
transcription_output,
retrieved_image_output,
enhanced_image_output,
retrieved_description_output,
clip_score_output,
],
)
# Launch Gradio Interface
if __name__ == "__main__":
demo.launch() |