File size: 2,868 Bytes
ca60315
 
60efbef
ca60315
 
3240d72
ca60315
60efbef
ca60315
 
 
 
ab6e199
 
 
 
 
 
 
 
60efbef
ab6e199
 
60efbef
ca60315
 
 
 
8948c2d
ec02482
85dcbec
ca60315
2d328f9
ca60315
2d328f9
ca60315
ec02482
 
ca60315
 
453fd37
ca60315
 
 
 
8948c2d
2544a9f
3240d72
ca60315
 
 
 
 
8948c2d
60efbef
8948c2d
60efbef
 
 
 
 
 
 
 
ca60315
60efbef
8948c2d
60efbef
ca60315
 
60efbef
 
 
 
ca60315
60efbef
ca60315
 
 
60efbef
 
ca60315
453fd37
ca60315
60efbef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import requests
import io
from PIL import Image, UnidentifiedImageError
import gradio as gr
from transformers import MarianMTModel, MarianTokenizer
import os


model_name = "Helsinki-NLP/opus-mt-mul-en"
model = MarianMTModel.from_pretrained(model_name)
tokenizer = MarianTokenizer.from_pretrained(model_name)

def translate_text(input_text, language):
    language_map = {
        "Tamil": "ta",
        "French": "fr",
        "Hindi": "hi",
        "German": "de"
    }
    
    lang_prefix = f">>{language_map[language]}<< "  
    text_with_lang = lang_prefix + input_text
    inputs = tokenizer(text_with_lang, return_tensors="pt", padding=True)

    translated_tokens = model.generate(**inputs)
    translation = tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
    return translation


def query_gemini_api(translated_text, gemini_api_key):
    url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
    headers = {"Content-Type": "application/json"}
    prompt = f"Based on the following sentence, continue the story: {translated_text}"
    payload = {
        "contents": [{"parts": [{"text": prompt}]}]
    }
    response = requests.post(f"{url}?key={gemini_api_key}", headers=headers, json=payload)

    if response.status_code == 200:
        result = response.json()
        creative_text = result['candidates'][0]['content']['parts'][0]['text']
        return creative_text
    else:
        return f"Error: {response.status_code} - {response.text}"


def query_image(payload):
    huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
    API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
    headers = {"Authorization": f"Bearer {huggingface_api_key}"}
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.content


def process_input(tamil_input, language):
    gemini_api_key = os.getenv('GEMINI_API_KEY')  
    translated_output = translate_text(tamil_input, language)
    creative_output = query_gemini_api(translated_output, gemini_api_key)
    image_bytes = query_image({"inputs": translated_output})

    try:
        image = Image.open(io.BytesIO(image_bytes))
    except UnidentifiedImageError:
        image = None  

    return translated_output, creative_output, image

# Gradio interface setup
iface = gr.Interface(
    fn=process_input,
    inputs=[
        gr.Textbox(label="Input Text"),
        gr.Dropdown(label="Select Language", choices=["Tamil", "French", "Hindi", "German"])
    ],
    outputs=[
        gr.Textbox(label="Translated Text"),
        gr.Textbox(label="Creative Text"),
        gr.Image(label="Generated Image")
    ],
    title="TRANSART🎨 BY Sakthi",
    description="Enter text to translate into English and generate an image based on the translated text."
)

iface.launch()