Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,92 +1,72 @@
|
|
1 |
-
import
|
2 |
-
from transformers import MBartForConditionalGeneration, AutoTokenizer, AutoModelForCausalLM, pipeline
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import io
|
6 |
from PIL import Image
|
7 |
import os
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
"
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
# Define the
|
23 |
-
|
24 |
-
tokenizer = AutoTokenizer.from_pretrained(translation_model_name)
|
25 |
-
translation_model = MBartForConditionalGeneration.from_pretrained(translation_model_name)
|
26 |
|
27 |
-
# Load a text generation model
|
28 |
-
text_generation_model_name = "EleutherAI/gpt-neo-
|
29 |
text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
|
30 |
-
text_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name
|
31 |
|
32 |
-
# Create a pipeline for text generation
|
33 |
text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer)
|
34 |
|
35 |
# Function to generate an image using Hugging Face's text-to-image model
|
36 |
def generate_image_from_text(translated_text):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
#
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
49 |
else:
|
50 |
-
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
55 |
tokenizer.src_lang = src_lang
|
56 |
encoded_input = tokenizer(input_text, return_tensors="pt")
|
57 |
-
|
58 |
-
|
59 |
-
return translated_text
|
60 |
-
|
61 |
-
# Function to generate text using the GPT-Neo model
|
62 |
-
def generate_text(prompt, max_length=50):
|
63 |
-
generated_texts = text_generator(prompt, max_length=max_length, num_return_sequences=1)
|
64 |
-
return generated_texts[0]["generated_text"]
|
65 |
-
|
66 |
-
# Define the Gradio Interface
|
67 |
-
def app_interface(input_text, src_language="en"):
|
68 |
-
translated_text = translate_text(input_text, src_lang=src_language)
|
69 |
-
generated_image = generate_image_from_text(translated_text)
|
70 |
-
generated_text = generate_text(translated_text)
|
71 |
-
return generated_text, generated_image
|
72 |
-
|
73 |
-
# Launch the Gradio App using the new Gradio components
|
74 |
-
with gr.Blocks() as demo:
|
75 |
-
gr.Markdown("# Multilingual Text-to-Image & Text Generation")
|
76 |
-
|
77 |
-
# Define Gradio components
|
78 |
-
input_text = gr.Textbox(lines=2, placeholder="Enter text here...")
|
79 |
-
src_language = gr.Dropdown(["en", "fr", "de", "es"], value="en", label="Source Language")
|
80 |
-
|
81 |
-
# Display outputs for text and image generation
|
82 |
-
generated_text_output = gr.Textbox(label="Generated Text")
|
83 |
-
generated_image_output = gr.Image(label="Generated Image")
|
84 |
-
|
85 |
-
# Button to trigger the processing
|
86 |
-
generate_button = gr.Button("Generate")
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
90 |
|
91 |
-
#
|
92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import MBartForConditionalGeneration, MBart50Tokenizer, AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import io
|
5 |
from PIL import Image
|
6 |
import os
|
7 |
|
8 |
+
# Load the translation model and tokenizer
|
9 |
+
model_name = "facebook/mbart-large-50-many-to-one-mmt"
|
10 |
+
tokenizer = MBart50Tokenizer.from_pretrained(model_name)
|
11 |
+
model = MBartForConditionalGeneration.from_pretrained(model_name)
|
|
|
12 |
|
13 |
+
# Use the Hugging Face API key from environment variables for text-to-image model
|
14 |
+
hf_api_key = os.getenv("full_token")
|
15 |
+
if hf_api_key is None:
|
16 |
+
raise ValueError("Hugging Face API key not found! Please set 'full_token' environment variable.")
|
17 |
+
else:
|
18 |
+
headers = {"Authorization": f"Bearer {hf_api_key}"}
|
19 |
|
20 |
+
# Define the text-to-image model URL (using a faster text-to-image model)
|
21 |
+
API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
|
|
|
|
|
22 |
|
23 |
+
# Load a smaller text generation model to reduce generation time
|
24 |
+
text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
|
25 |
text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
|
26 |
+
text_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name)
|
27 |
|
28 |
+
# Create a pipeline for text generation using the selected model
|
29 |
text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer)
|
30 |
|
31 |
# Function to generate an image using Hugging Face's text-to-image model
|
32 |
def generate_image_from_text(translated_text):
|
33 |
+
try:
|
34 |
+
# Enhanced prompt to focus on details and clarity
|
35 |
+
enhanced_prompt = f"A high-quality image of a person doing yoga with clear facial features and correct body proportions in a tranquil outdoor setting. " \
|
36 |
+
f"Include detailed mountains, flowing river, and vibrant greenery, captured in soft sunrise light. Ensure the face and body are realistic and proportional."
|
37 |
+
|
38 |
+
print(f"Generating image from translated text: {enhanced_prompt}")
|
39 |
+
|
40 |
+
# Sending the enhanced prompt to the text-to-image model
|
41 |
+
response = requests.post(API_URL, headers=headers, json={"inputs": enhanced_prompt})
|
42 |
+
if response.status_code == 200:
|
43 |
+
image_data = response.content
|
44 |
+
image = Image.open(io.BytesIO(image_data))
|
45 |
+
return image
|
46 |
else:
|
47 |
+
raise ValueError(f"Error in image generation: {response.text}")
|
48 |
|
49 |
+
except Exception as e:
|
50 |
+
print(f"Error: {e}")
|
51 |
+
return None
|
52 |
+
|
53 |
+
# Translation Function
|
54 |
+
def translate_text(input_text, src_lang="en_XX", tgt_lang="hi_IN"):
|
55 |
tokenizer.src_lang = src_lang
|
56 |
encoded_input = tokenizer(input_text, return_tensors="pt")
|
57 |
+
generated_tokens = model.generate(encoded_input["input_ids"], forced_bos_token_id=tokenizer.lang_code_to_id[tgt_lang])
|
58 |
+
return tokenizer.batch_decode(generated_tokens, skip_special_tokens=True)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Gradio Interface for image generation
|
61 |
+
def translate_and_generate_image(input_text):
|
62 |
+
translated_text = translate_text(input_text)
|
63 |
+
image = generate_image_from_text(translated_text)
|
64 |
+
return image
|
65 |
|
66 |
+
# Create a simple Gradio Interface
|
67 |
+
iface = gr.Interface(fn=translate_and_generate_image,
|
68 |
+
inputs="text",
|
69 |
+
outputs="image",
|
70 |
+
title="Yoga Image Generator",
|
71 |
+
description="Enter a description to translate and generate a high-quality yoga image.")
|
72 |
+
iface.launch()
|