|
|
|
import gradio as gr |
|
import requests |
|
from getpass import getpass |
|
import openai |
|
from PIL import Image |
|
import io |
|
|
|
|
|
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: ") |
|
|
|
|
|
openai.api_key = getpass("Enter OpenAI API Key: ") |
|
|
|
|
|
|
|
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_url = "https://api-inference.huggingface.co/models/facebook/mbart-large-50-many-to-one-mmt" |
|
|
|
|
|
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_model = "black-forest-labs/FLUX.1-schnell" |
|
|
|
|
|
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_model = "GPT-4 (OpenAI)" |
|
|
|
|
|
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}" |
|
|
|
|
|
def generate_content(english_text, max_tokens, temperature, model): |
|
if model == "gpt-4": |
|
|
|
response = openai.Completion.create( |
|
engine=model, |
|
prompt=f"Write educational content about {english_text} within {max_tokens} tokens.", |
|
max_tokens=max_tokens, |
|
temperature=temperature |
|
) |
|
return response.choices[0].text.strip() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
|
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}" |
|
|
|
|
|
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}" |
|
|
|
|
|
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: |
|
|
|
image = Image.open(io.BytesIO(response.content)) |
|
|
|
image.save("/tmp/generated_image.png") |
|
return "/tmp/generated_image.png" |
|
else: |
|
return f"Image Generation Error {response.status_code}: {response.text}" |
|
|
|
|
|
def fusionmind_app(tamil_input, temperature, max_tokens, content_model, image_model): |
|
|
|
english_text = translate_text(tamil_input) |
|
|
|
|
|
content_output = generate_content(english_text, max_tokens, temperature, content_models[content_model]) |
|
|
|
|
|
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 |
|
|
|
|
|
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") |
|
], |
|
title="TransArt: A Multimodal Application for Vernacular Language Translation and Image Synthesis", |
|
description="Translate Tamil to English, generate educational content, and generate related images!" |
|
) |
|
|
|
|
|
interface.launch(debug=True) |
|
|