Update app.py
Browse files
app.py
CHANGED
@@ -1,182 +1,47 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
import openai
|
6 |
-
from PIL import Image
|
7 |
-
import io
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
Image_Token = getpass("Enter Hugging Face Image Generation Token: ")
|
12 |
-
Content_Token = getpass("Enter Groq Content Generation Token: ")
|
13 |
-
Image_prompt_token = getpass("Enter Groq Prompt Generation Token: ")
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
# gemini_token = getpass("Enter Gemini API Key: ") # Placeholder, you will need API access
|
18 |
|
19 |
-
|
20 |
-
Translate
|
21 |
-
Image_generation = {"Authorization": f"Bearer {Image_Token}"}
|
22 |
-
Content_generation = {
|
23 |
-
"Authorization": f"Bearer {Content_Token}",
|
24 |
-
"Content-Type": "application/json"
|
25 |
-
}
|
26 |
-
Image_Prompt = {
|
27 |
-
"Authorization": f"Bearer {Image_prompt_token}",
|
28 |
-
"Content-Type": "application/json"
|
29 |
-
}
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
"CompVis/stable-diffusion-v1-4": "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4",
|
38 |
-
"black-forest-labs/FLUX.1-dev": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
39 |
-
}
|
40 |
-
|
41 |
-
# Default image generation model
|
42 |
-
default_image_model = "black-forest-labs/FLUX.1-schnell"
|
43 |
-
|
44 |
-
# Content generation models
|
45 |
-
content_models = {
|
46 |
-
"GPT-4 (OpenAI)": "gpt-4",
|
47 |
-
"Gemini-1 (DeepMind)": "gemini-1",
|
48 |
-
"llama-3.1-70b-versatile": "llama-3.1-70b-versatile",
|
49 |
-
"mixtral-8x7b-32768": "mixtral-8x7b-32768"
|
50 |
-
}
|
51 |
-
|
52 |
-
# Default content generation model
|
53 |
-
default_content_model = "GPT-4 (OpenAI)"
|
54 |
-
|
55 |
-
# Function to query Hugging Face translation model
|
56 |
-
def translate_text(text):
|
57 |
-
payload = {"inputs": text}
|
58 |
-
response = requests.post(translation_url, headers=Translate, json=payload)
|
59 |
-
if response.status_code == 200:
|
60 |
-
result = response.json()
|
61 |
-
translated_text = result[0]['generated_text']
|
62 |
-
return translated_text
|
63 |
-
else:
|
64 |
-
return f"Translation Error {response.status_code}: {response.text}"
|
65 |
-
|
66 |
-
# Function to generate content using GPT or Gemini
|
67 |
-
def generate_content(english_text, max_tokens, temperature, model):
|
68 |
-
if model == "gpt-4":
|
69 |
-
# Using OpenAI's GPT model
|
70 |
-
response = openai.Completion.create(
|
71 |
-
engine=model, # GPT model (like gpt-4)
|
72 |
-
prompt=f"Write educational content about {english_text} within {max_tokens} tokens.",
|
73 |
-
max_tokens=max_tokens,
|
74 |
-
temperature=temperature
|
75 |
-
)
|
76 |
-
return response.choices[0].text.strip()
|
77 |
-
|
78 |
-
# elif model == "gemini-1":
|
79 |
-
# # Placeholder: Add code to call Gemini API here
|
80 |
-
# # Using the Gemini API (this requires the correct endpoint and token from Google DeepMind)
|
81 |
-
# # For example, you would create a POST request similar to OpenAI's API.
|
82 |
-
# url = "https://api.deepmind.com/gemini/v1/generate"
|
83 |
-
# headers = {
|
84 |
-
# "Authorization": f"Bearer {gemini_token}",
|
85 |
-
# "Content-Type": "application/json"
|
86 |
-
# }
|
87 |
-
# payload = {
|
88 |
-
# "model": "gemini-1",
|
89 |
-
# "input": f"Write educational content about {english_text} within {max_tokens} tokens.",
|
90 |
-
# "temperature": temperature,
|
91 |
-
# "max_tokens": max_tokens
|
92 |
-
# }
|
93 |
-
# response = requests.post(url, json=payload, headers=headers)
|
94 |
-
# if response.status_code == 200:
|
95 |
-
# return response.json()['choices'][0]['text']
|
96 |
-
# else:
|
97 |
-
# return f"Gemini Content Generation Error {response.status_code}: {response.text}"
|
98 |
-
|
99 |
-
else:
|
100 |
-
# Default to the Groq API or other models if selected
|
101 |
-
url = "https://api.groq.com/openai/v1/chat/completions"
|
102 |
-
payload = {
|
103 |
-
"model": model,
|
104 |
-
"messages": [
|
105 |
-
{"role": "system", "content": "You are a creative and insightful writer."},
|
106 |
-
{"role": "user", "content": f"Write educational content about {english_text} within {max_tokens} tokens."}
|
107 |
-
],
|
108 |
-
"max_tokens": max_tokens,
|
109 |
-
"temperature": temperature
|
110 |
-
}
|
111 |
-
response = requests.post(url, json=payload, headers=Content_generation)
|
112 |
-
if response.status_code == 200:
|
113 |
-
result = response.json()
|
114 |
-
return result['choices'][0]['message']['content']
|
115 |
-
else:
|
116 |
-
return f"Content Generation Error: {response.status_code}"
|
117 |
-
|
118 |
-
# Function to generate image prompt
|
119 |
-
def generate_image_prompt(english_text):
|
120 |
-
payload = {
|
121 |
-
"model": "mixtral-8x7b-32768",
|
122 |
-
"messages": [
|
123 |
-
{"role": "system", "content": "You are a professional Text to image prompt generator."},
|
124 |
-
{"role": "user", "content": f"Create a text to image generation prompt about {english_text} within 30 tokens."}
|
125 |
-
],
|
126 |
-
"max_tokens": 30
|
127 |
-
}
|
128 |
-
response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=payload, headers=Image_Prompt)
|
129 |
-
if response.status_code == 200:
|
130 |
-
result = response.json()
|
131 |
-
return result['choices'][0]['message']['content']
|
132 |
-
else:
|
133 |
-
return f"Prompt Generation Error: {response.status_code}"
|
134 |
-
|
135 |
-
# Function to generate an image from the prompt
|
136 |
-
def generate_image(image_prompt, model_url):
|
137 |
-
data = {"inputs": image_prompt}
|
138 |
-
response = requests.post(model_url, headers=Image_generation, json=data)
|
139 |
-
if response.status_code == 200:
|
140 |
-
# Convert the image bytes to a PIL Image
|
141 |
-
image = Image.open(io.BytesIO(response.content))
|
142 |
-
# Save image to a temporary file-like object for Gradio
|
143 |
-
image.save("/tmp/generated_image.png") # Save the image to a file
|
144 |
-
return "/tmp/generated_image.png" # Return the path to the image
|
145 |
else:
|
146 |
-
|
|
|
|
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
# Step 1: Translation (Tamil to English)
|
151 |
-
english_text = translate_text(tamil_input)
|
152 |
|
153 |
-
|
154 |
-
|
|
|
|
|
155 |
|
156 |
-
|
157 |
-
|
158 |
-
image_data = generate_image(image_prompt, image_generation_urls[image_model])
|
159 |
|
160 |
-
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
-
|
163 |
-
interface = gr.Interface(
|
164 |
-
fn=fusionmind_app,
|
165 |
-
inputs=[
|
166 |
-
gr.Textbox(label="Enter Tamil Text"),
|
167 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperature"),
|
168 |
-
gr.Slider(minimum=100, maximum=400, value=200, label="Max Tokens for Content Generation"),
|
169 |
-
gr.Dropdown(list(content_models.keys()), label="Select Content Generation Model", value=default_content_model),
|
170 |
-
gr.Dropdown(list(image_generation_urls.keys()), label="Select Image Generation Model", value=default_image_model)
|
171 |
-
],
|
172 |
-
outputs=[
|
173 |
-
gr.Textbox(label="Translated English Text"),
|
174 |
-
gr.Textbox(label="Generated Content"),
|
175 |
-
gr.Image(label="Generated Image") # Display the generated image
|
176 |
-
],
|
177 |
-
title="TransArt: A Multimodal Application for Vernacular Language Translation and Image Synthesis",
|
178 |
-
description="Translate Tamil to English, generate educational content, and generate related images!"
|
179 |
-
)
|
180 |
|
181 |
-
|
182 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
from diffusers import StableDiffusionPipeline
|
4 |
+
import torch
|
|
|
|
|
|
|
5 |
|
6 |
+
# Hugging Face token from secrets
|
7 |
+
HF_TOKEN = st.secrets["HF_TOKEN"]
|
|
|
|
|
|
|
8 |
|
9 |
+
# Streamlit page settings
|
10 |
+
st.set_page_config(page_title="Tamil to Image Generator", layout="centered")
|
|
|
11 |
|
12 |
+
st.title("🧠 Tamil to Image Generator 🎨")
|
13 |
+
st.markdown("Enter Tamil text → Translate to English → Generate a creative story → Create an AI Image")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Input Tamil text
|
16 |
+
tamil_input = st.text_area("Enter Tamil text", placeholder="உலகின் அழகான கடற்கரை பற்றி சொல்...")
|
17 |
|
18 |
+
if st.button("Generate Image"):
|
19 |
+
if not tamil_input.strip():
|
20 |
+
st.warning("Please enter some Tamil text.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
else:
|
22 |
+
with st.spinner("🔁 Translating Tamil to English..."):
|
23 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-ta-en")
|
24 |
+
translated = translator(tamil_input, max_length=100)[0]['translation_text']
|
25 |
|
26 |
+
st.success("✅ Translation done")
|
27 |
+
st.markdown(f"**📝 Translated Text:** `{translated}`")
|
|
|
|
|
28 |
|
29 |
+
with st.spinner("🧠 Generating creative English text..."):
|
30 |
+
generator = pipeline("text-generation", model="gpt2")
|
31 |
+
prompt_text = f"Describe this beautifully: {translated}"
|
32 |
+
generated = generator(prompt_text, max_length=80, do_sample=True, top_k=50)[0]['generated_text']
|
33 |
|
34 |
+
st.success("✅ Text generation done")
|
35 |
+
st.markdown(f"**🎨 Creative Description:** `{generated}`")
|
|
|
36 |
|
37 |
+
with st.spinner("🖼️ Generating AI Image... (may take 20–30 seconds)"):
|
38 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
39 |
+
"runwayml/stable-diffusion-v1-5",
|
40 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
41 |
+
use_auth_token=HF_TOKEN
|
42 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
43 |
|
44 |
+
image = pipe(prompt=generated).images[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
st.success("✅ Image generated!")
|
47 |
+
st.image(image, caption="🖼️ AI Generated Image", use_column_width=True)
|