pravin0077 commited on
Commit
39ba994
·
verified ·
1 Parent(s): 3201c4f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -51
app.py CHANGED
@@ -1,74 +1,77 @@
 
1
  import requests
2
- import io
3
  from PIL import Image
 
4
  import gradio as gr
5
- from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
6
- import os
7
 
8
- # Load the translation model
9
  model_name = "Helsinki-NLP/opus-mt-mul-en"
10
  translation_model = MarianMTModel.from_pretrained(model_name)
11
  translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
12
 
13
- # Load GPT-2 model and tokenizer (smaller and faster than GPT-Neo)
14
- gpt_model_name = "gpt2"
15
- gpt_tokenizer = AutoTokenizer.from_pretrained(gpt_model_name)
16
- gpt_model = AutoModelForCausalLM.from_pretrained(gpt_model_name)
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
 
18
  def translate_text(tamil_text):
19
- inputs = translation_tokenizer(tamil_text, return_tensors="pt")
20
  translated_tokens = translation_model.generate(**inputs)
21
  translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
22
  return translation
23
 
24
- def query_gpt_2(translated_text):
25
- prompt = f"Continue the story based on the following text: {translated_text}"
26
- inputs = gpt_tokenizer(prompt, return_tensors="pt")
27
- outputs = gpt_model.generate(inputs['input_ids'], max_length=50, num_return_sequences=1) # Reduced max_length for speed
28
- creative_text = gpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
29
- return creative_text
30
 
31
- def query_image(payload):
32
- huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
33
- if not huggingface_api_key:
34
- return "Error: Hugging Face API key not set."
 
 
35
 
36
- API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
37
- headers = {"Authorization": f"Bearer {huggingface_api_key}"}
38
- response = requests.post(API_URL, headers=headers, json=payload)
 
39
 
40
- if response.status_code == 200:
41
- return response.content
42
- else:
43
- return f"Error: {response.status_code} - {response.text}"
44
-
45
- def process_input(tamil_input):
46
- try:
47
- # Translate the input text
48
- translated_output = translate_text(tamil_input)
49
-
50
- # Generate creative text using GPT-2
51
- creative_output = query_gpt_2(translated_output)
52
-
53
- # Generate an image using Hugging Face's FLUX model
54
- image_bytes = query_image({"inputs": translated_output})
55
- image = Image.open(io.BytesIO(image_bytes))
56
-
57
- return translated_output, creative_output, image
58
- except Exception as e:
59
- return f"Error occurred: {str(e)}", "", None
60
 
61
- # Create a Gradio interface
62
  interface = gr.Interface(
63
- fn=process_input,
64
- inputs=[gr.Textbox(label="Input Tamil Text")],
65
  outputs=[
66
- gr.Textbox(label="Translated Text"),
67
- gr.Textbox(label="Creative Text"),
68
- gr.Image(label="Generated Image")
69
  ],
70
- title="TRANSART",
71
- description="Enter Tamil text to translate to English and generate an image based on the translated text."
72
  )
73
 
74
- interface.launch()
 
 
1
+ from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
2
  import requests
 
3
  from PIL import Image
4
+ import io
5
  import gradio as gr
 
 
6
 
7
+ # Load the MarianMT model and tokenizer for translation (Tamil to English)
8
  model_name = "Helsinki-NLP/opus-mt-mul-en"
9
  translation_model = MarianMTModel.from_pretrained(model_name)
10
  translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
11
 
12
+ # Load GPT-Neo for creative text generation
13
+ text_generation_model_name = "EleutherAI/gpt-neo-1.3B"
14
+ text_generation_model = AutoModelForCausalLM.from_pretrained(text_generation_model_name)
15
+ text_generation_tokenizer = AutoTokenizer.from_pretrained(text_generation_model_name)
16
+
17
+ # Add padding token to GPT-Neo tokenizer if not present
18
+ if text_generation_tokenizer.pad_token is None:
19
+ text_generation_tokenizer.add_special_tokens({'pad_token': '[PAD]'})
20
+
21
+ # Hugging Face API for FLUX.1 image generation
22
+ API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
23
+ headers = {"Authorization": "HUGGINGFACE_API_KEY"} # Replace with your API key
24
+
25
+ # Query Hugging Face API to generate image
26
+ def query(payload):
27
+ response = requests.post(API_URL, headers=headers, json=payload)
28
+ return response.content
29
 
30
+ # Translate Tamil text to English
31
  def translate_text(tamil_text):
32
+ inputs = translation_tokenizer(tamil_text, return_tensors="pt", padding=True, truncation=True)
33
  translated_tokens = translation_model.generate(**inputs)
34
  translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
35
  return translation
36
 
37
+ # Generate an image based on the translated text
38
+ def generate_image(prompt):
39
+ image_bytes = query({"inputs": prompt})
40
+ image = Image.open(io.BytesIO(image_bytes))
41
+ return image
 
42
 
43
+ # Generate creative text based on the translated English text
44
+ def generate_creative_text(translated_text):
45
+ inputs = text_generation_tokenizer(translated_text, return_tensors="pt", padding=True, truncation=True)
46
+ generated_tokens = text_generation_model.generate(**inputs, max_length=100)
47
+ creative_text = text_generation_tokenizer.decode(generated_tokens[0], skip_special_tokens=True)
48
+ return creative_text
49
 
50
+ # Function to handle the full workflow
51
+ def translate_generate_image_and_text(tamil_text):
52
+ # Step 1: Translate Tamil to English
53
+ translated_text = translate_text(tamil_text)
54
 
55
+ # Step 2: Generate an image from the translated text
56
+ image = generate_image(translated_text)
57
+
58
+ # Step 3: Generate creative text from the translated text
59
+ creative_text = generate_creative_text(translated_text)
60
+
61
+ return translated_text, creative_text, image
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ # Create Gradio interface
64
  interface = gr.Interface(
65
+ fn=translate_generate_image_and_text,
66
+ inputs=gr.Textbox(label="Enter Tamil Text"), # Input for Tamil text
67
  outputs=[
68
+ gr.Textbox(label="Translated Text"), # Output for translated text
69
+ gr.Textbox(label="Creative Generated Text"),# Output for creative text
70
+ gr.Image(label="Generated Image") # Output for generated image
71
  ],
72
+ title="Tamil to English Translation, Image Generation & Creative Text",
73
+ description="Enter Tamil text to translate to English, generate an image, and create creative text based on the translation."
74
  )
75
 
76
+ # Launch Gradio app
77
+ interface.launch()