gokilashree commited on
Commit
ae23d7c
·
verified ·
1 Parent(s): 625f3ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -17
app.py CHANGED
@@ -4,7 +4,6 @@ import requests
4
  import io
5
  from PIL import Image
6
  import os
7
- import time
8
 
9
  # Load the translation model and tokenizer
10
  model_name = "facebook/mbart-large-50-many-to-one-mmt"
@@ -18,15 +17,15 @@ if hf_api_key is None:
18
  else:
19
  headers = {"Authorization": f"Bearer {hf_api_key}"}
20
 
21
- # Define the text-to-image model URL (using a stable diffusion model)
22
  API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
23
 
24
- # Load the text generation model for generating detailed paragraphs
25
- text_generation_model_name = "EleutherAI/gpt-neo-2.7B"
26
  text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
27
  text_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name)
28
 
29
- # Create a pipeline for text generation
30
  text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer)
31
 
32
  # Function to generate an image using Hugging Face's text-to-image model
@@ -49,19 +48,19 @@ def generate_image_from_text(translated_text):
49
  print(f"Error during image generation: {e}")
50
  return None, f"Error during image generation: {e}"
51
 
52
- # Function to generate a detailed paragraph from the translated text
53
- def generate_paragraph_from_text(translated_text):
54
  try:
55
- print(f"Generating paragraph from translated text: {translated_text}")
56
- # Generate detailed text from translated text using the text generation model
57
- paragraph = text_generator(translated_text, max_length=250, num_return_sequences=1, temperature=0.7, top_p=0.9)[0]['generated_text']
58
  print(f"Paragraph generation completed: {paragraph}")
59
  return paragraph
60
  except Exception as e:
61
  print(f"Error during paragraph generation: {e}")
62
  return f"Error during paragraph generation: {e}"
63
 
64
- # Define the function to translate Tamil text, generate a paragraph, and create an image
65
  def translate_generate_paragraph_and_image(tamil_text):
66
  # Step 1: Translate Tamil text to English using mbart-large-50
67
  try:
@@ -74,8 +73,8 @@ def translate_generate_paragraph_and_image(tamil_text):
74
  except Exception as e:
75
  return f"Error during translation: {e}", "", None, None
76
 
77
- # Step 2: Generate a detailed paragraph based on the translated English text
78
- paragraph = generate_paragraph_from_text(translated_text)
79
  if "Error" in paragraph:
80
  return translated_text, paragraph, None, None
81
 
@@ -91,11 +90,11 @@ iface = gr.Interface(
91
  fn=translate_generate_paragraph_and_image,
92
  inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text here..."),
93
  outputs=[gr.Textbox(label="Translated English Text"),
94
- gr.Textbox(label="Generated Descriptive Paragraph"),
95
  gr.Image(label="Generated Image")],
96
- title="Tamil to English Translation, Paragraph Generation, and Image Creation",
97
- description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate a detailed paragraph, and create an image using the translated text.",
98
  )
99
 
100
  # Launch Gradio app without `share=True`
101
- iface.launch()
 
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"
 
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
 
48
  print(f"Error during image generation: {e}")
49
  return None, f"Error during image generation: {e}"
50
 
51
+ # Function to generate a shorter paragraph based on the translated text
52
+ def generate_short_paragraph_from_text(translated_text):
53
  try:
54
+ print(f"Generating a short paragraph from translated text: {translated_text}")
55
+ # Generate a shorter paragraph from the translated text using smaller settings
56
+ paragraph = text_generator(translated_text, max_length=100, num_return_sequences=1, temperature=0.7, top_p=0.8)[0]['generated_text']
57
  print(f"Paragraph generation completed: {paragraph}")
58
  return paragraph
59
  except Exception as e:
60
  print(f"Error during paragraph generation: {e}")
61
  return f"Error during paragraph generation: {e}"
62
 
63
+ # Define the function to translate Tamil text, generate a short paragraph, and create an image
64
  def translate_generate_paragraph_and_image(tamil_text):
65
  # Step 1: Translate Tamil text to English using mbart-large-50
66
  try:
 
73
  except Exception as e:
74
  return f"Error during translation: {e}", "", None, None
75
 
76
+ # Step 2: Generate a shorter paragraph based on the translated English text
77
+ paragraph = generate_short_paragraph_from_text(translated_text)
78
  if "Error" in paragraph:
79
  return translated_text, paragraph, None, None
80
 
 
90
  fn=translate_generate_paragraph_and_image,
91
  inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text here..."),
92
  outputs=[gr.Textbox(label="Translated English Text"),
93
+ gr.Textbox(label="Generated Short Paragraph"),
94
  gr.Image(label="Generated Image")],
95
+ title="Tamil to English Translation, Short Paragraph Generation, and Image Creation",
96
+ description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate a short paragraph, and create an image using the translated text.",
97
  )
98
 
99
  # Launch Gradio app without `share=True`
100
+ iface.launch()