Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,60 +2,78 @@ 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 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def translate_text(tamil_text):
|
13 |
-
inputs =
|
14 |
-
translated_tokens =
|
15 |
-
translation =
|
16 |
return translation
|
17 |
|
18 |
-
def
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
}
|
25 |
-
response = requests.post(f"{url}?key={gemini_api_key}", headers=headers, json=payload)
|
26 |
-
|
27 |
-
if response.status_code == 200:
|
28 |
-
result = response.json()
|
29 |
-
creative_text = result['candidates'][0]['content']['parts'][0]['text']
|
30 |
-
return creative_text
|
31 |
-
else:
|
32 |
-
return f"Error: {response.status_code} - {response.text}"
|
33 |
|
34 |
def query_image(payload):
|
35 |
huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
|
|
|
|
|
|
|
36 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
37 |
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
38 |
response = requests.post(API_URL, headers=headers, json=payload)
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
creative_output = query_gemini_api(translated_output, gemini_api_key)
|
45 |
-
image_bytes = query_image({"inputs": translated_output})
|
46 |
-
image = Image.open(io.BytesIO(image_bytes))
|
47 |
-
return translated_output, creative_output, image
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
|
|
50 |
interface = gr.Interface(
|
51 |
fn=process_input,
|
52 |
-
inputs=[
|
|
|
|
|
|
|
53 |
outputs=[
|
54 |
gr.Textbox(label="Translated Text"),
|
55 |
gr.Textbox(label="Creative Text"),
|
56 |
gr.Image(label="Generated Image")
|
57 |
],
|
58 |
-
title="TRANSART",
|
59 |
-
description="Enter Tamil text to translate to English and
|
|
|
|
|
60 |
)
|
|
|
61 |
interface.launch()
|
|
|
2 |
import io
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
+
from transformers import MarianMTModel, MarianTokenizer, AutoModelForCausalLM, AutoTokenizer
|
6 |
import os
|
7 |
|
8 |
+
# Load the translation model
|
9 |
model_name = "Helsinki-NLP/opus-mt-mul-en"
|
10 |
+
translation_model = MarianMTModel.from_pretrained(model_name)
|
11 |
+
translation_tokenizer = MarianTokenizer.from_pretrained(model_name)
|
12 |
+
|
13 |
+
# Load GPT-Neo model and tokenizer
|
14 |
+
gpt_model_name = "EleutherAI/gpt-neo-1.3B" # You can also use gpt-neo-2.7B if needed
|
15 |
+
gpt_tokenizer = AutoTokenizer.from_pretrained(gpt_model_name)
|
16 |
+
gpt_model = AutoModelForCausalLM.from_pretrained(gpt_model_name)
|
17 |
|
18 |
def translate_text(tamil_text):
|
19 |
+
inputs = translation_tokenizer(tamil_text, return_tensors="pt")
|
20 |
+
translated_tokens = translation_model.generate(**inputs)
|
21 |
+
translation = translation_tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
22 |
return translation
|
23 |
|
24 |
+
def query_gpt_neo(translated_text, max_words):
|
25 |
+
prompt = f"Continue the story based on the following text: {translated_text}"
|
26 |
+
inputs = gpt_tokenizer(prompt, return_tensors="pt")
|
27 |
+
outputs = gpt_model.generate(inputs['input_ids'], max_length=max_words, num_return_sequences=1)
|
28 |
+
creative_text = gpt_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
29 |
+
return creative_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def query_image(payload):
|
32 |
huggingface_api_key = os.getenv('HUGGINGFACE_API_KEY')
|
33 |
+
if not huggingface_api_key:
|
34 |
+
return "Error: Hugging Face API key not set."
|
35 |
+
|
36 |
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
37 |
headers = {"Authorization": f"Bearer {huggingface_api_key}"}
|
38 |
response = requests.post(API_URL, headers=headers, json=payload)
|
39 |
+
|
40 |
+
if response.status_code == 200:
|
41 |
+
return response.content
|
42 |
+
else:
|
43 |
+
return f"Error: {response.status_code} - {response.text}"
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
def process_input(tamil_input, max_words):
|
46 |
+
try:
|
47 |
+
# Translate the input text
|
48 |
+
translated_output = translate_text(tamil_input)
|
49 |
+
|
50 |
+
# Generate creative text using GPT-Neo
|
51 |
+
creative_output = query_gpt_neo(translated_output, max_words)
|
52 |
+
|
53 |
+
# Generate an image using Hugging Face's FLUX model
|
54 |
+
image_bytes = query_image({"inputs": translated_output})
|
55 |
+
image = Image.open(io.BytesIO(image_bytes))
|
56 |
+
|
57 |
+
return translated_output, creative_output, image
|
58 |
+
except Exception as e:
|
59 |
+
return f"Error occurred: {str(e)}", "", None
|
60 |
|
61 |
+
# Create a Gradio interface with interactive elements
|
62 |
interface = gr.Interface(
|
63 |
fn=process_input,
|
64 |
+
inputs=[
|
65 |
+
gr.Textbox(label="Input Tamil Text", placeholder="Enter your Tamil text here..."),
|
66 |
+
gr.Slider(label="Max Words for Creative Text", minimum=50, maximum=200, step=10, value=100)
|
67 |
+
],
|
68 |
outputs=[
|
69 |
gr.Textbox(label="Translated Text"),
|
70 |
gr.Textbox(label="Creative Text"),
|
71 |
gr.Image(label="Generated Image")
|
72 |
],
|
73 |
+
title="TRANSART - Multimodal AI App",
|
74 |
+
description="Enter Tamil text to translate to English, generate creative text, and produce an image based on the translated text.",
|
75 |
+
theme="compact", # Use the 'compact' theme for a cleaner app look
|
76 |
+
layout="vertical" # Arrange components vertically for better readability
|
77 |
)
|
78 |
+
|
79 |
interface.launch()
|