Raveheart1 commited on
Commit
07380a1
·
verified ·
1 Parent(s): f2209f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -4,24 +4,23 @@ from PIL import Image
4
  import gradio as gr
5
  from transformers import MarianMTModel, MarianTokenizer
6
 
7
- # Load the translation model
8
  model_name = "Helsinki-NLP/opus-mt-mul-en"
9
  model = MarianMTModel.from_pretrained(model_name)
10
  tokenizer = MarianTokenizer.from_pretrained(model_name)
11
 
12
- # Function to translate Tamil text to English
13
  def translate_text(tamil_text):
14
  inputs = tokenizer(tamil_text, return_tensors="pt")
15
  translated_tokens = model.generate(**inputs)
16
  translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
17
  return translation
18
 
19
- # Function to query Gemini API for creative text
20
  def query_gemini_api(translated_text, gemini_api_key):
21
  url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
22
  headers = {"Content-Type": "application/json"}
 
23
  payload = {
24
- "contents": [{"parts": [{"text": translated_text}]}]
25
  }
26
  response = requests.post(f"{url}?key={gemini_api_key}", headers=headers, json=payload)
27
 
@@ -32,14 +31,15 @@ def query_gemini_api(translated_text, gemini_api_key):
32
  else:
33
  return f"Error: {response.status_code} - {response.text}"
34
 
35
- # Function to query the Hugging Face API for image generation
 
36
  def query_image(payload, huggingface_api_key):
37
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
38
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
39
  response = requests.post(API_URL, headers=headers, json=payload)
40
  return response.content
41
 
42
- # Main function to process input
43
  def process_input(tamil_input, gemini_api_key, huggingface_api_key):
44
  translated_output = translate_text(tamil_input)
45
  creative_output = query_gemini_api(translated_output, gemini_api_key)
@@ -47,13 +47,13 @@ def process_input(tamil_input, gemini_api_key, huggingface_api_key):
47
  image = Image.open(io.BytesIO(image_bytes))
48
  return translated_output, creative_output, image
49
 
50
- # Gradio interface
51
  iface = gr.Interface(
52
  fn=process_input,
53
  inputs=[
54
  gr.Textbox(label="Input Tamil Text"),
55
  gr.Textbox(label="Enter your Gemini API Key", type="password"),
56
- gr.Textbox(label="Enter your Hugging Face API Key", type="password")
57
  ],
58
  outputs=[
59
  gr.Textbox(label="Translated Text"),
@@ -63,5 +63,4 @@ iface = gr.Interface(
63
  title="TRANSART🎨 BY Sakthi",
64
  description="Enter Tamil text to translate to English and generate an image based on the translated text."
65
  )
66
-
67
  iface.launch()
 
4
  import gradio as gr
5
  from transformers import MarianMTModel, MarianTokenizer
6
 
 
7
  model_name = "Helsinki-NLP/opus-mt-mul-en"
8
  model = MarianMTModel.from_pretrained(model_name)
9
  tokenizer = MarianTokenizer.from_pretrained(model_name)
10
 
11
+
12
  def translate_text(tamil_text):
13
  inputs = tokenizer(tamil_text, return_tensors="pt")
14
  translated_tokens = model.generate(**inputs)
15
  translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
16
  return translation
17
 
 
18
  def query_gemini_api(translated_text, gemini_api_key):
19
  url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
20
  headers = {"Content-Type": "application/json"}
21
+ prompt = f"Based on the following sentence, continue the story: {translated_text}"
22
  payload = {
23
+ "contents": [{"parts": [{"text": prompt}]}]
24
  }
25
  response = requests.post(f"{url}?key={gemini_api_key}", headers=headers, json=payload)
26
 
 
31
  else:
32
  return f"Error: {response.status_code} - {response.text}"
33
 
34
+
35
+
36
  def query_image(payload, huggingface_api_key):
37
  API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
38
  headers = {"Authorization": f"Bearer {huggingface_api_key}"}
39
  response = requests.post(API_URL, headers=headers, json=payload)
40
  return response.content
41
 
42
+
43
  def process_input(tamil_input, gemini_api_key, huggingface_api_key):
44
  translated_output = translate_text(tamil_input)
45
  creative_output = query_gemini_api(translated_output, gemini_api_key)
 
47
  image = Image.open(io.BytesIO(image_bytes))
48
  return translated_output, creative_output, image
49
 
50
+
51
  iface = gr.Interface(
52
  fn=process_input,
53
  inputs=[
54
  gr.Textbox(label="Input Tamil Text"),
55
  gr.Textbox(label="Enter your Gemini API Key", type="password"),
56
+ gr.Textbox(label="Enter your Hugging Face API Key", type="password"),
57
  ],
58
  outputs=[
59
  gr.Textbox(label="Translated Text"),
 
63
  title="TRANSART🎨 BY Sakthi",
64
  description="Enter Tamil text to translate to English and generate an image based on the translated text."
65
  )
 
66
  iface.launch()