gokilashree commited on
Commit
eb723d1
·
verified ·
1 Parent(s): 6dcce25

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Set up the Hugging Face API key from environment variables
9
+ hf_api_key = os.getenv("new_hf_token")
10
+ if not hf_api_key:
11
+ raise ValueError("Hugging Face API key not found! Please set the 'HF_API_KEY' environment variable.")
12
+ headers = {"Authorization": f"Bearer {hf_api_key}"}
13
+
14
+ # Define the text-to-image model URL
15
+ API_URL = "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4"
16
+
17
+ # Load the translation model and tokenizer
18
+ translation_model_name = "facebook/mbart-large-50-many-to-one-mmt"
19
+ tokenizer = MBart50Tokenizer.from_pretrained(translation_model_name)
20
+ translation_model = MBartForConditionalGeneration.from_pretrained(translation_model_name)
21
+
22
+ # Load a text generation model from Hugging Face
23
+ text_generation_model_name = "EleutherAI/gpt-neo-2.7B" # Use "EleutherAI/gpt-j-6B" for better quality
24
+ text_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
25
+ text_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name, device_map="auto", torch_dtype=torch.float32)
26
+
27
+ # Create a pipeline for text generation
28
+ text_generator = pipeline("text-generation", model=text_model, tokenizer=text_tokenizer)
29
+
30
+ # Function to generate an image using Hugging Face's text-to-image model
31
+ def generate_image_from_text(translated_text):
32
+ try:
33
+ # Send the translated text to the text-to-image model
34
+ response = requests.post(API_URL, headers=headers, json={"inputs": translated_text})
35
+
36
+ # Check if the response is successful
37
+ if response.status_code != 200:
38
+ return None, f"Error generating image: {response.text}"
39
+
40
+ # Read and return the generated image
41
+ image_bytes = response.content
42
+ image = Image.open(io.BytesIO(image_bytes))
43
+ return image, None
44
+ except Exception as e:
45
+ return None, f"Error during image generation: {e}"
46
+
47
+ # Define the function to translate Tamil text, generate an image, and create a descriptive text
48
+ def translate_generate_image_and_text(tamil_text):
49
+ try:
50
+ # Step 1: Translate Tamil text to English
51
+ tokenizer.src_lang = "ta_IN"
52
+ inputs = tokenizer(tamil_text, return_tensors="pt")
53
+ translated_tokens = translation_model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
54
+ translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
55
+ except Exception as e:
56
+ return f"Error during translation: {e}", None, None
57
+
58
+ try:
59
+ # Step 2: Use the translated English text to generate an image directly
60
+ image, error_message = generate_image_from_text(translated_text)
61
+ if error_message:
62
+ return translated_text, None, error_message
63
+ except Exception as e:
64
+ return translated_text, None, f"Error during image generation: {e}"
65
+
66
+ try:
67
+ # Step 3: Generate a descriptive English text using GPT-Neo based on the translated text
68
+ descriptive_text = text_generator(translated_text, max_length=100, num_return_sequences=1, temperature=0.7, top_p=0.9)[0]['generated_text']
69
+ except Exception as e:
70
+ return translated_text, image, f"Error during text generation: {e}"
71
+
72
+ return translated_text, image, descriptive_text
73
+
74
+ # Gradio interface setup
75
+ iface = gr.Interface(
76
+ fn=translate_generate_image_and_text,
77
+ inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text here..."),
78
+ outputs=[gr.Textbox(label="Translated English Text"),
79
+ gr.Image(label="Generated Image"),
80
+ gr.Textbox(label="Generated Descriptive Text")],
81
+ title="Tamil to English Translation, Image Creation, and Descriptive Text Generation",
82
+ description="Translate Tamil text to English using Facebook's mbart-large-50 model, create an image using the translated text, and generate a descriptive text based on the translated content.",
83
+ )
84
+
85
+ # Launch the Gradio app
86
+ iface.launch()