Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
import requests
|
3 |
+
import io
|
4 |
+
from PIL import Image
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
from transformers import MarianMTModel, MarianTokenizer, pipeline
|
7 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
# Constants for model names and API URLs
|
11 |
+
class Constants:
|
12 |
+
TRANSLATION_MODEL_NAME = "Helsinki-NLP/opus-mt-mul-en"
|
13 |
+
IMAGE_GENERATION_API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
14 |
+
GPT_NEO_MODEL_NAME = "EleutherAI/gpt-neo-125M"
|
15 |
+
HEADERS = {"Authorization": "Bearer hf_token"}
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
# Translation Class
|
20 |
+
class Translator:
|
21 |
+
def __init__(self):
|
22 |
+
self.tokenizer = MarianTokenizer.from_pretrained(Constants.TRANSLATION_MODEL_NAME)
|
23 |
+
self.model = MarianMTModel.from_pretrained(Constants.TRANSLATION_MODEL_NAME)
|
24 |
+
self.pipeline = pipeline("translation", model=self.model, tokenizer=self.tokenizer)
|
25 |
+
|
26 |
+
def translate(self, tamil_text):
|
27 |
+
"""Translate Tamil text to English."""
|
28 |
+
try:
|
29 |
+
translation = self.pipeline(tamil_text, max_length=40)
|
30 |
+
return translation[0]['translation_text']
|
31 |
+
except Exception as e:
|
32 |
+
return f"Translation error: {str(e)}"
|
33 |
+
|
34 |
+
|
35 |
+
# Image Generation Class
|
36 |
+
class ImageGenerator:
|
37 |
+
def __init__(self):
|
38 |
+
self.api_url = Constants.IMAGE_GENERATION_API_URL
|
39 |
+
|
40 |
+
def generate(self, prompt):
|
41 |
+
"""Generate an image based on the given prompt."""
|
42 |
+
try:
|
43 |
+
response = requests.post(self.api_url, headers=Constants.HEADERS, json={"inputs": prompt})
|
44 |
+
if response.status_code == 200:
|
45 |
+
image_bytes = response.content
|
46 |
+
return Image.open(io.BytesIO(image_bytes))
|
47 |
+
else:
|
48 |
+
print(f"Image generation failed: Status code {response.status_code}")
|
49 |
+
return None
|
50 |
+
except Exception as e:
|
51 |
+
print(f"Image generation error: {str(e)}")
|
52 |
+
return None
|
53 |
+
|
54 |
+
|
55 |
+
# Creative Text Generation Class
|
56 |
+
class CreativeTextGenerator:
|
57 |
+
def __init__(self):
|
58 |
+
self.tokenizer = AutoTokenizer.from_pretrained(Constants.GPT_NEO_MODEL_NAME)
|
59 |
+
self.model = AutoModelForCausalLM.from_pretrained(Constants.GPT_NEO_MODEL_NAME)
|
60 |
+
|
61 |
+
def generate(self, translated_text):
|
62 |
+
"""Generate creative text based on translated text."""
|
63 |
+
input_ids = self.tokenizer(translated_text, return_tensors='pt').input_ids
|
64 |
+
generated_text_ids = self.model.generate(input_ids, max_length=100)
|
65 |
+
return self.tokenizer.decode(generated_text_ids[0], skip_special_tokens=True)
|
66 |
+
|
67 |
+
|
68 |
+
# Main Application Class
|
69 |
+
class TransArtApp:
|
70 |
+
def __init__(self):
|
71 |
+
self.translator = Translator()
|
72 |
+
self.image_generator = ImageGenerator()
|
73 |
+
self.creative_text_generator = CreativeTextGenerator()
|
74 |
+
|
75 |
+
def process(self, tamil_text):
|
76 |
+
"""Handle the full workflow: translate, generate image, and creative text."""
|
77 |
+
translated_text = self.translator.translate(tamil_text)
|
78 |
+
image = self.image_generator.generate(translated_text)
|
79 |
+
creative_text = self.creative_text_generator.generate(translated_text)
|
80 |
+
return translated_text, creative_text, image
|
81 |
+
|
82 |
+
|
83 |
+
# Function to display images
|
84 |
+
def show_image(image):
|
85 |
+
"""Display an image using matplotlib."""
|
86 |
+
if image:
|
87 |
+
plt.imshow(image)
|
88 |
+
plt.axis('off') # Hide axes
|
89 |
+
plt.show()
|
90 |
+
else:
|
91 |
+
print("No image to display.")
|
92 |
+
|
93 |
+
|
94 |
+
# Create an instance of the TransArt app
|
95 |
+
app = TransArtApp()
|
96 |
+
|
97 |
+
# Gradio interface function
|
98 |
+
def gradio_interface(tamil_text):
|
99 |
+
"""Interface function for Gradio."""
|
100 |
+
translated_text, creative_text, image = app.process(tamil_text)
|
101 |
+
return translated_text, creative_text, image
|
102 |
+
|
103 |
+
|
104 |
+
# Create Gradio interface
|
105 |
+
interface = gr.Interface(
|
106 |
+
fn=gradio_interface,
|
107 |
+
inputs="text",
|
108 |
+
outputs=["text", "text", "image"],
|
109 |
+
title="Tamil to English Translation, Image Generation & Creative Text",
|
110 |
+
description="Enter Tamil text to translate to English, generate an image, and create creative text based on the translation."
|
111 |
+
)
|
112 |
+
|
113 |
+
# Launch Gradio app
|
114 |
+
if __name__ == "__main__":
|
115 |
+
interface.launch()
|