Spaces:
Runtime error
Runtime error
File size: 2,978 Bytes
37409eb 1a79b75 37409eb 1a79b75 d0fda9d 1a79b75 37409eb 1a79b75 37409eb 1a79b75 37409eb 1a79b75 37409eb d0fda9d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import openai
from transformers import MBartForConditionalGeneration, MBart50Tokenizer
import gradio as gr
import requests
import io
from PIL import Image
import os
# Set up your OpenAI API key (make sure it's stored as an environment variable)
openai.api_key = os.getenv("OPENAI_API_KEY")
# Load the translation model and tokenizer
model_name = "facebook/mbart-large-50-many-to-one-mmt"
tokenizer = MBart50Tokenizer.from_pretrained(model_name)
model = MBartForConditionalGeneration.from_pretrained(model_name)
# Use the Hugging Face API key from environment variables for text-to-image model
API_URL = "https://api-inference.huggingface.co/models/ZB-Tech/Text-to-Image"
headers = {"Authorization": f"Bearer {os.getenv('full_token')}"}
# Define the OpenAI GPT-3 text generation function
def generate_with_gpt3(prompt, max_tokens=150, temperature=0.7):
response = openai.Completion.create(
engine="text-davinci-003", # You can also use "text-davinci-002" or "curie"
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature,
top_p=0.9,
frequency_penalty=0.0,
presence_penalty=0.0
)
return response.choices[0].text.strip()
# Define the translation, GPT-3 text generation, and image generation function
def translate_and_generate_image(tamil_text):
# Step 1: Translate Tamil text to English using mbart-large-50
tokenizer.src_lang = "ta_IN"
inputs = tokenizer(tamil_text, return_tensors="pt")
translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id["en_XX"])
translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
# Step 2: Generate high-quality descriptive text using OpenAI's GPT-3
prompt = f"Create a detailed and creative description based on the following text: {translated_text}"
generated_text = generate_with_gpt3(prompt, max_tokens=150, temperature=0.7)
# Step 3: Use the generated English text to create an image
def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.content
# Generate image using the generated text
image_bytes = query({"inputs": generated_text})
image = Image.open(io.BytesIO(image_bytes))
return translated_text, generated_text, image
# Gradio interface setup
iface = gr.Interface(
fn=translate_and_generate_image,
inputs=gr.Textbox(lines=2, placeholder="Enter Tamil text here..."),
outputs=[gr.Textbox(label="Translated English Text"),
gr.Textbox(label="Generated Descriptive Text"),
gr.Image(label="Generated Image")],
title="Tamil to English Translation, GPT-3 Text Generation, and Image Creation",
description="Translate Tamil text to English using Facebook's mbart-large-50 model, generate high-quality text using GPT-3, and create an image using the generated text.",
)
# Launch Gradio app without `share=True`
iface.launch()
|