gokilashree commited on
Commit
95431fa
·
verified ·
1 Parent(s): 0501446

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -71
app.py CHANGED
@@ -1,92 +1,72 @@
1
- import torch
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
- # Set up the Hugging Face API key from environment variables
10
- hf_api_key = os.getenv("new_hf_token")
11
- if not hf_api_key:
12
- raise ValueError("Hugging Face API key not found! Please set the 'HF_API_KEY' environment variable.")
13
- headers = {"Authorization": f"Bearer {hf_api_key}"}
14
 
15
- # Define the text-to-image model URLs
16
- model_urls = {
17
- "stable_diffusion_v1_4": "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4",
18
- "stable_diffusion_v1_5": "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
19
- }
20
- API_URL = model_urls["stable_diffusion_v1_4"]
21
 
22
- # Define the translation model for multilingual text inputs
23
- translation_model_name = "facebook/mbart-large-50-many-to-one-mmt"
24
- tokenizer = AutoTokenizer.from_pretrained(translation_model_name)
25
- translation_model = MBartForConditionalGeneration.from_pretrained(translation_model_name)
26
 
27
- # Load a text generation model from Hugging Face
28
- text_generation_model_name = "EleutherAI/gpt-neo-2.7B"
29
  text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
30
- text_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name, device_map="auto", torch_dtype=torch.float32)
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
- payload = {"inputs": translated_text, "options": {"wait_for_model": True}}
38
- response = requests.post(API_URL, headers=headers, json=payload)
39
- if response.status_code == 200:
40
- image_data = response.content
41
- image = Image.open(io.BytesIO(image_data))
42
- return image
43
- else:
44
- # If the model is loading, check the estimated wait time
45
- if response.status_code == 503:
46
- error_message = response.json()
47
- estimated_time = error_message.get("estimated_time", "Unknown")
48
- return f"Model is currently loading. Estimated wait time: {estimated_time} seconds. Try again later."
 
49
  else:
50
- return f"Failed to generate image. Error: {response.status_code}, Message: {response.text}"
51
 
52
- # Function to translate text using the MBart model
53
- def translate_text(input_text, src_lang="en"):
54
- # Tokenize and translate
 
 
 
55
  tokenizer.src_lang = src_lang
56
  encoded_input = tokenizer(input_text, return_tensors="pt")
57
- translated_tokens = translation_model.generate(**encoded_input)
58
- translated_text = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
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
- # Link the button to the function call
89
- generate_button.click(fn=app_interface, inputs=[input_text, src_language], outputs=[generated_text_output, generated_image_output])
 
 
 
90
 
91
- # Run the app
92
- demo.launch()
 
 
 
 
 
 
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()