Create TransArt.py
Browse files- TransArt.py +182 -0
TransArt.py
ADDED
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import required libraries
|
2 |
+
import gradio as gr
|
3 |
+
import requests
|
4 |
+
from getpass import getpass
|
5 |
+
import openai
|
6 |
+
from PIL import Image
|
7 |
+
import io
|
8 |
+
|
9 |
+
# Input your Hugging Face and Groq tokens securely
|
10 |
+
Transalate_token = getpass("Enter Hugging Face Translation Token: ")
|
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 |
+
# API Keys for GPT and Gemini (replace with your actual keys)
|
16 |
+
openai.api_key = getpass("Enter OpenAI API Key: ")
|
17 |
+
# gemini_token = getpass("Enter Gemini API Key: ") # Placeholder, you will need API access
|
18 |
+
|
19 |
+
# API Headers
|
20 |
+
Translate = {"Authorization": f"Bearer {Transalate_token}"}
|
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 |
+
# Translation Model API URL (Tamil to English)
|
32 |
+
translation_url = "https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-one-mmt"
|
33 |
+
|
34 |
+
# Text-to-Image Model API URLs
|
35 |
+
image_generation_urls = {
|
36 |
+
"black-forest-labs/FLUX.1-schnell": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",
|
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 |
+
return f"Image Generation Error {response.status_code}: {response.text}"
|
147 |
+
|
148 |
+
# Gradio App
|
149 |
+
def fusionmind_app(tamil_input, temperature, max_tokens, content_model, image_model):
|
150 |
+
# Step 1: Translation (Tamil to English)
|
151 |
+
english_text = translate_text(tamil_input)
|
152 |
+
|
153 |
+
# Step 2: Generate Educational Content
|
154 |
+
content_output = generate_content(english_text, max_tokens, temperature, content_models[content_model])
|
155 |
+
|
156 |
+
# Step 3: Generate Image from the prompt
|
157 |
+
image_prompt = generate_image_prompt(english_text)
|
158 |
+
image_data = generate_image(image_prompt, image_generation_urls[image_model])
|
159 |
+
|
160 |
+
return english_text, content_output, image_data
|
161 |
+
|
162 |
+
# Gradio Interface
|
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 |
+
# Launch Gradio App
|
182 |
+
interface.launch(debug=True)
|