TransArt / TransArt.py
geetika14's picture
Create TransArt.py
0c0223c verified
# Import required libraries
import gradio as gr
import requests
from getpass import getpass
import openai
from PIL import Image
import io
# Input your Hugging Face and Groq tokens securely
Transalate_token = getpass("Enter Hugging Face Translation Token: ")
Image_Token = getpass("Enter Hugging Face Image Generation Token: ")
Content_Token = getpass("Enter Groq Content Generation Token: ")
Image_prompt_token = getpass("Enter Groq Prompt Generation Token: ")
# API Keys for GPT and Gemini (replace with your actual keys)
openai.api_key = getpass("Enter OpenAI API Key: ")
# gemini_token = getpass("Enter Gemini API Key: ") # Placeholder, you will need API access
# API Headers
Translate = {"Authorization": f"Bearer {Transalate_token}"}
Image_generation = {"Authorization": f"Bearer {Image_Token}"}
Content_generation = {
"Authorization": f"Bearer {Content_Token}",
"Content-Type": "application/json"
}
Image_Prompt = {
"Authorization": f"Bearer {Image_prompt_token}",
"Content-Type": "application/json"
}
# Translation Model API URL (Tamil to English)
translation_url = "https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-one-mmt"
# Text-to-Image Model API URLs
image_generation_urls = {
"black-forest-labs/FLUX.1-schnell": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-schnell",
"CompVis/stable-diffusion-v1-4": "https://api-inference.huggingface.co/models/CompVis/stable-diffusion-v1-4",
"black-forest-labs/FLUX.1-dev": "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
}
# Default image generation model
default_image_model = "black-forest-labs/FLUX.1-schnell"
# Content generation models
content_models = {
"GPT-4 (OpenAI)": "gpt-4",
"Gemini-1 (DeepMind)": "gemini-1",
"llama-3.1-70b-versatile": "llama-3.1-70b-versatile",
"mixtral-8x7b-32768": "mixtral-8x7b-32768"
}
# Default content generation model
default_content_model = "GPT-4 (OpenAI)"
# Function to query Hugging Face translation model
def translate_text(text):
payload = {"inputs": text}
response = requests.post(translation_url, headers=Translate, json=payload)
if response.status_code == 200:
result = response.json()
translated_text = result[0]['generated_text']
return translated_text
else:
return f"Translation Error {response.status_code}: {response.text}"
# Function to generate content using GPT or Gemini
def generate_content(english_text, max_tokens, temperature, model):
if model == "gpt-4":
# Using OpenAI's GPT model
response = openai.Completion.create(
engine=model, # GPT model (like gpt-4)
prompt=f"Write educational content about {english_text} within {max_tokens} tokens.",
max_tokens=max_tokens,
temperature=temperature
)
return response.choices[0].text.strip()
# elif model == "gemini-1":
# # Placeholder: Add code to call Gemini API here
# # Using the Gemini API (this requires the correct endpoint and token from Google DeepMind)
# # For example, you would create a POST request similar to OpenAI's API.
# url = "https://api.deepmind.com/gemini/v1/generate"
# headers = {
# "Authorization": f"Bearer {gemini_token}",
# "Content-Type": "application/json"
# }
# payload = {
# "model": "gemini-1",
# "input": f"Write educational content about {english_text} within {max_tokens} tokens.",
# "temperature": temperature,
# "max_tokens": max_tokens
# }
# response = requests.post(url, json=payload, headers=headers)
# if response.status_code == 200:
# return response.json()['choices'][0]['text']
# else:
# return f"Gemini Content Generation Error {response.status_code}: {response.text}"
else:
# Default to the Groq API or other models if selected
url = "https://api.groq.com/openai/v1/chat/completions"
payload = {
"model": model,
"messages": [
{"role": "system", "content": "You are a creative and insightful writer."},
{"role": "user", "content": f"Write educational content about {english_text} within {max_tokens} tokens."}
],
"max_tokens": max_tokens,
"temperature": temperature
}
response = requests.post(url, json=payload, headers=Content_generation)
if response.status_code == 200:
result = response.json()
return result['choices'][0]['message']['content']
else:
return f"Content Generation Error: {response.status_code}"
# Function to generate image prompt
def generate_image_prompt(english_text):
payload = {
"model": "mixtral-8x7b-32768",
"messages": [
{"role": "system", "content": "You are a professional Text to image prompt generator."},
{"role": "user", "content": f"Create a text to image generation prompt about {english_text} within 30 tokens."}
],
"max_tokens": 30
}
response = requests.post("https://api.groq.com/openai/v1/chat/completions", json=payload, headers=Image_Prompt)
if response.status_code == 200:
result = response.json()
return result['choices'][0]['message']['content']
else:
return f"Prompt Generation Error: {response.status_code}"
# Function to generate an image from the prompt
def generate_image(image_prompt, model_url):
data = {"inputs": image_prompt}
response = requests.post(model_url, headers=Image_generation, json=data)
if response.status_code == 200:
# Convert the image bytes to a PIL Image
image = Image.open(io.BytesIO(response.content))
# Save image to a temporary file-like object for Gradio
image.save("/tmp/generated_image.png") # Save the image to a file
return "/tmp/generated_image.png" # Return the path to the image
else:
return f"Image Generation Error {response.status_code}: {response.text}"
# Gradio App
def fusionmind_app(tamil_input, temperature, max_tokens, content_model, image_model):
# Step 1: Translation (Tamil to English)
english_text = translate_text(tamil_input)
# Step 2: Generate Educational Content
content_output = generate_content(english_text, max_tokens, temperature, content_models[content_model])
# Step 3: Generate Image from the prompt
image_prompt = generate_image_prompt(english_text)
image_data = generate_image(image_prompt, image_generation_urls[image_model])
return english_text, content_output, image_data
# Gradio Interface
interface = gr.Interface(
fn=fusionmind_app,
inputs=[
gr.Textbox(label="Enter Tamil Text"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperature"),
gr.Slider(minimum=100, maximum=400, value=200, label="Max Tokens for Content Generation"),
gr.Dropdown(list(content_models.keys()), label="Select Content Generation Model", value=default_content_model),
gr.Dropdown(list(image_generation_urls.keys()), label="Select Image Generation Model", value=default_image_model)
],
outputs=[
gr.Textbox(label="Translated English Text"),
gr.Textbox(label="Generated Content"),
gr.Image(label="Generated Image") # Display the generated image
],
title="TransArt: A Multimodal Application for Vernacular Language Translation and Image Synthesis",
description="Translate Tamil to English, generate educational content, and generate related images!"
)
# Launch Gradio App
interface.launch(debug=True)