Raveheart1 commited on
Commit
8948c2d
·
verified ·
1 Parent(s): ab6e199

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -14
app.py CHANGED
@@ -10,9 +10,9 @@ model_name = "Helsinki-NLP/opus-mt-mul-en"
10
  model = MarianMTModel.from_pretrained(model_name)
11
  tokenizer = MarianTokenizer.from_pretrained(model_name)
12
 
13
- # Define a function for language translation
14
  def translate_text(input_text, language):
15
- # Dictionary mapping the selected language to the respective language tag
16
  language_map = {
17
  "Tamil": "ta",
18
  "French": "fr",
@@ -20,17 +20,17 @@ def translate_text(input_text, language):
20
  "German": "de"
21
  }
22
 
23
- # Prepare input with the appropriate language code for the model
24
- lang_prefix = f">>{language_map[language]}<< " # Add the prefix needed for the model
25
  text_with_lang = lang_prefix + input_text
26
  inputs = tokenizer(text_with_lang, return_tensors="pt", padding=True)
27
 
28
- # Perform translation
29
  translated_tokens = model.generate(**inputs)
30
  translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
31
  return translation
32
 
33
- # Function to query the Gemini API for creative text generation
34
  def query_gemini_api(translated_text, gemini_api_key):
35
  url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
36
  headers = {"Content-Type": "application/json"}
@@ -47,7 +47,7 @@ def query_gemini_api(translated_text, gemini_api_key):
47
  else:
48
  return f"Error: {response.status_code} - {response.text}"
49
 
50
- # Function to query the image generation API
51
  def query_image(payload):
52
  huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
53
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
@@ -55,17 +55,17 @@ def query_image(payload):
55
  response = requests.post(API_URL, headers=headers, json=payload)
56
  return response.content
57
 
58
- # Function to process the entire input
59
  def process_input(input_text, language):
60
- gemini_api_key = os.getenv('GEMINI_API_KEY') # Use your Gemini API key
61
- translated_output = translate_text(input_text, language) # Translate text from selected language to English
62
- creative_output = query_gemini_api(translated_output, gemini_api_key) # Generate creative text
63
- image_bytes = query_image({"inputs": translated_output}) # Generate an image based on the translated text
64
- image = Image.open(io.BytesIO(image_bytes)) # Convert bytes to image
65
 
66
  return translated_output, creative_output, image
67
 
68
- # Create Gradio Interface
69
  iface = gr.Interface(
70
  fn=process_input,
71
  inputs=[gr.Textbox(label="Enter text in the selected language"),
 
10
  model = MarianMTModel.from_pretrained(model_name)
11
  tokenizer = MarianTokenizer.from_pretrained(model_name)
12
 
13
+
14
  def translate_text(input_text, language):
15
+
16
  language_map = {
17
  "Tamil": "ta",
18
  "French": "fr",
 
20
  "German": "de"
21
  }
22
 
23
+
24
+ lang_prefix = f">>{language_map[language]}<<
25
  text_with_lang = lang_prefix + input_text
26
  inputs = tokenizer(text_with_lang, return_tensors="pt", padding=True)
27
 
28
+
29
  translated_tokens = model.generate(**inputs)
30
  translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
31
  return translation
32
 
33
+
34
  def query_gemini_api(translated_text, gemini_api_key):
35
  url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
36
  headers = {"Content-Type": "application/json"}
 
47
  else:
48
  return f"Error: {response.status_code} - {response.text}"
49
 
50
+
51
  def query_image(payload):
52
  huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
53
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
 
55
  response = requests.post(API_URL, headers=headers, json=payload)
56
  return response.content
57
 
58
+
59
  def process_input(input_text, language):
60
+ gemini_api_key = os.getenv('GEMINI_API_KEY')
61
+ translated_output = translate_text(input_text, language)
62
+ creative_output = query_gemini_api(translated_output, gemini_api_key)
63
+ image_bytes = query_image({"inputs": translated_output})
64
+ image = Image.open(io.BytesIO(image_bytes))
65
 
66
  return translated_output, creative_output, image
67
 
68
+
69
  iface = gr.Interface(
70
  fn=process_input,
71
  inputs=[gr.Textbox(label="Enter text in the selected language"),