Spaces:
Runtime error
Runtime error
Raveheart1
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +1,22 @@
|
|
1 |
import requests
|
2 |
import io
|
3 |
-
import os
|
4 |
from PIL import Image
|
5 |
import gradio as gr
|
6 |
from transformers import MarianMTModel, MarianTokenizer
|
|
|
7 |
|
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 |
-
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
13 |
-
huggingface_api_key = os.getenv("HUGGINGFACE_API_KEY")
|
14 |
-
|
15 |
-
|
16 |
def translate_text(tamil_text):
|
17 |
inputs = tokenizer(tamil_text, return_tensors="pt")
|
18 |
translated_tokens = model.generate(**inputs)
|
19 |
translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
20 |
return translation
|
21 |
|
22 |
-
def query_gemini_api(translated_text,
|
|
|
23 |
url = f"https://generativelanguage.googleapis.com/v1beta2/models/gemini-1.5:generateText?key={gemini_api_key}"
|
24 |
headers = {"Content-Type": "application/json"}
|
25 |
payload = {
|
@@ -38,23 +35,25 @@ def query_gemini_api(translated_text, gemini_api_key, max_output_tokens=300):
|
|
38 |
return f"Error: {response.status_code} - {response.text}"
|
39 |
|
40 |
def query_image(payload):
|
|
|
41 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
42 |
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
43 |
response = requests.post(API_URL, headers=headers, json=payload)
|
44 |
return response.content
|
45 |
|
46 |
-
|
47 |
-
def process_input(tamil_input):
|
48 |
translated_output = translate_text(tamil_input)
|
49 |
-
creative_output = query_gemini_api(translated_output)
|
50 |
image_bytes = query_image({"inputs": translated_output})
|
51 |
image = Image.open(io.BytesIO(image_bytes))
|
52 |
return translated_output, creative_output, image
|
53 |
|
54 |
-
|
55 |
iface = gr.Interface(
|
56 |
fn=process_input,
|
57 |
-
inputs=
|
|
|
|
|
|
|
58 |
outputs=[
|
59 |
gr.Textbox(label="Translated Text"),
|
60 |
gr.Textbox(label="Creative Text"),
|
|
|
1 |
import requests
|
2 |
import io
|
|
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
from transformers import MarianMTModel, MarianTokenizer
|
6 |
+
import os
|
7 |
|
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 |
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, max_output_tokens=300):
|
19 |
+
gemini_api_key = os.getenv('GEMINI_API_KEY')
|
20 |
url = f"https://generativelanguage.googleapis.com/v1beta2/models/gemini-1.5:generateText?key={gemini_api_key}"
|
21 |
headers = {"Content-Type": "application/json"}
|
22 |
payload = {
|
|
|
35 |
return f"Error: {response.status_code} - {response.text}"
|
36 |
|
37 |
def query_image(payload):
|
38 |
+
huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
|
39 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
40 |
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
41 |
response = requests.post(API_URL, headers=headers, json=payload)
|
42 |
return response.content
|
43 |
|
44 |
+
def process_input(tamil_input, max_output_tokens):
|
|
|
45 |
translated_output = translate_text(tamil_input)
|
46 |
+
creative_output = query_gemini_api(translated_output, max_output_tokens)
|
47 |
image_bytes = query_image({"inputs": translated_output})
|
48 |
image = Image.open(io.BytesIO(image_bytes))
|
49 |
return translated_output, creative_output, image
|
50 |
|
|
|
51 |
iface = gr.Interface(
|
52 |
fn=process_input,
|
53 |
+
inputs=[
|
54 |
+
gr.Textbox(label="Input Tamil Text"),
|
55 |
+
gr.Slider(50, 1000, value=300, step=50, label="Max Output Tokens for Creative Text")
|
56 |
+
],
|
57 |
outputs=[
|
58 |
gr.Textbox(label="Translated Text"),
|
59 |
gr.Textbox(label="Creative Text"),
|