Spaces:
Running
Running
saivarun04
commited on
Commit
•
b9ba5bf
1
Parent(s):
4e5f25e
Upload 2 files
Browse files- app.py +103 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
from transformers import MarianMTModel, MarianTokenizer
|
4 |
+
import google.generativeai as genai
|
5 |
+
from huggingface_hub import InferenceApi, login
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Load API key from environment variable
|
9 |
+
os.environ["API_KEY"] = os.getenv("GOOGLE_API_KEY") # Set your Google API key in your environment variables
|
10 |
+
genai.configure(api_key=os.environ["API_KEY"]) # Configure Google Generative AI with the API key
|
11 |
+
|
12 |
+
# Load Hugging Face token from environment variable
|
13 |
+
hf_token = os.getenv("HF_TOK") # Set your Hugging Face token in your environment variables
|
14 |
+
login(hf_token) # Log in to Hugging Face using the token
|
15 |
+
|
16 |
+
# Initialize the diffusion model for image generation
|
17 |
+
diffusion_model = InferenceApi(repo_id="black-forest-labs/FLUX.1-schnell")
|
18 |
+
|
19 |
+
# Initialize the generative model for creative writing
|
20 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
21 |
+
|
22 |
+
# Load the pre-trained model for Tamil to English translation
|
23 |
+
translator_model_name = "Helsinki-NLP/opus-mt-mul-en" # Model name for translation
|
24 |
+
tokenizer = MarianTokenizer.from_pretrained(translator_model_name) # Load the tokenizer for the translation model
|
25 |
+
translator = MarianMTModel.from_pretrained(translator_model_name) # Load the translation model
|
26 |
+
|
27 |
+
def translate_text(tamil_text):
|
28 |
+
"""
|
29 |
+
Translates Tamil text to English using the MarianMT model.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
tamil_text (str): Text in Tamil to be translated.
|
33 |
+
|
34 |
+
Returns:
|
35 |
+
str: Translated text in English.
|
36 |
+
"""
|
37 |
+
# Prepare the input for the translation model
|
38 |
+
inputs = tokenizer(tamil_text, return_tensors="pt", padding=True)
|
39 |
+
# Generate the translation
|
40 |
+
translated = translator.generate(**inputs)
|
41 |
+
# Decode the generated tokens to text
|
42 |
+
return tokenizer.decode(translated[0], skip_special_tokens=True)
|
43 |
+
|
44 |
+
def generate_creative_writing(english_text):
|
45 |
+
"""
|
46 |
+
Generates creative writing (e.g., a poem) based on the provided English text.
|
47 |
+
|
48 |
+
Args:
|
49 |
+
english_text (str): Text in English to generate creative content from.
|
50 |
+
|
51 |
+
Returns:
|
52 |
+
str: Generated creative writing.
|
53 |
+
"""
|
54 |
+
# Generate creative writing content
|
55 |
+
return model.generate_content("poem about " + english_text).text
|
56 |
+
|
57 |
+
def generate_image(prompt):
|
58 |
+
"""
|
59 |
+
Generates an image based on the provided prompt using a diffusion model.
|
60 |
+
|
61 |
+
Args:
|
62 |
+
prompt (str): Text prompt for image generation.
|
63 |
+
|
64 |
+
Returns:
|
65 |
+
Image: Generated image based on the prompt.
|
66 |
+
"""
|
67 |
+
# Make an inference request to generate an image
|
68 |
+
response = diffusion_model(inputs=prompt, params={"guidance_scale": 7.5, "num_inference_steps": 50})
|
69 |
+
return response # Ensure this is in the correct format for display
|
70 |
+
|
71 |
+
def process_input(tamil_text):
|
72 |
+
"""
|
73 |
+
Processes the input Tamil text to perform translation, creative writing generation,
|
74 |
+
and image generation.
|
75 |
+
|
76 |
+
Args:
|
77 |
+
tamil_text (str): Input text in Tamil.
|
78 |
+
|
79 |
+
Returns:
|
80 |
+
tuple: Translated text, creative writing response, and generated image or error message.
|
81 |
+
"""
|
82 |
+
try:
|
83 |
+
# Translate Tamil text to English
|
84 |
+
translated_text = translate_text(tamil_text)
|
85 |
+
# Generate creative writing based on the translated text
|
86 |
+
creative_response = generate_creative_writing(translated_text)
|
87 |
+
# Generate an image based on the translated text
|
88 |
+
generated_image = generate_image(translated_text)
|
89 |
+
return translated_text, creative_response, generated_image
|
90 |
+
except Exception as e:
|
91 |
+
return str(e), "Error occurred during processing", None # Return error information
|
92 |
+
|
93 |
+
# Create a Gradio interface for user interaction
|
94 |
+
iface = gr.Interface(
|
95 |
+
fn=process_input, # Function to process input
|
96 |
+
inputs="text", # Input type: text box
|
97 |
+
outputs=["text", "text", "image"], # Outputs: translated text, creative writing, and image
|
98 |
+
title="Creative Writing and Image Generation", # Title of the interface
|
99 |
+
description="Enter Tamil text to get translated text, a creative response, and an image." # Description of the interface
|
100 |
+
)
|
101 |
+
|
102 |
+
# Launch the Gradio app
|
103 |
+
iface.launch() # Start the Gradio interface for user interaction
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
diffusers
|
4 |
+
gradio
|
5 |
+
translate
|
6 |
+
google.generativeai
|
7 |
+
huggingface_hub
|
8 |
+
sentencepiece
|