Spaces:
Runtime error
Runtime error
Raveheart1
commited on
Update app.py
Browse files
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 |
-
|
14 |
def translate_text(input_text, language):
|
15 |
-
|
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 |
-
|
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,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 |
-
|
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 |
-
|
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"),
|
|
|
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"),
|