Spaces:
Runtime error
Runtime error
import gradio as gr | |
# from langchain.llms import OpenAI | |
from langchain_openai import OpenAI | |
from transformers import pipeline | |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel | |
import os | |
openai_api_key = os.getenv("OPENAI_API_KEY") | |
# Load text generation model | |
# text_generation_model = pipeline("text-generation", model="openai-community/gpt2-large") | |
# text_generation_model = pipeline("text-generation", model="distilbert/distilgpt2") | |
# Load image captioning model | |
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning" | |
decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning" | |
model_checkpoint = "nlpconnect/vit-gpt2-image-captioning" | |
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint) | |
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint) | |
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint) | |
def generate_story(image, theme, genre): | |
try: | |
# Preprocess the image | |
image = image.convert('RGB') | |
image_features = feature_extractor(images=image, return_tensors="pt") | |
# Generate image caption | |
caption_ids = model.generate(image_features.pixel_values, max_length=50, num_beams=3, temperature=1.0) | |
# Decode the caption | |
caption_text = tokenizer.batch_decode(caption_ids, skip_special_tokens=True)[0] | |
# Generate story based on the caption | |
story_prompt = f"Write an interesting {theme} story in the {genre} genre. The story should be within 100 words about {caption_text}." | |
llm = OpenAI(model_name="gpt-3.5-turbo-instruct", openai_api_key=openai_api_key) | |
story = llm.invoke(story_prompt) | |
# story = text_generation_model(story_prompt, max_length=150)[0]["generated_text"] | |
return caption_text, story | |
except Exception as e: | |
return f"An error occurred during inference: {str(e)}" | |
# Gradio interface | |
input_image = gr.Image(label="Select Image",type="pil") | |
input_theme = gr.Dropdown(["Love and Loss", "Identity and Self-Discovery", "Power and Corruption", "Redemption and Forgiveness", "Survival and Resilience", "Nature and the Environment", "Justice and Injustice", "Friendship and Loyalty", "Hope and Despair"], label="Input Theme") | |
input_genre = gr.Dropdown(["Fantasy", "Science Fiction", "Poetry", "Mystery/Thriller", "Romance", "Historical Fiction", "Horror", "Adventure", "Drama", "Comedy"], label="Input Genre") | |
output_caption = gr.Textbox(label="Image Caption", lines=2) | |
output_text = gr.Textbox(label="Generated Story",lines=8) | |
examples = [f"example{i}.jpg" for i in range(1,2)] | |
gr.Interface( | |
fn=generate_story, | |
inputs=[input_image, input_theme, input_genre], | |
outputs=[output_caption, output_text], | |
examples = examples, | |
title="Image to Story Generator", | |
description="Generate a story from an image taking theme and genre as input. It leverages image captioning and text generation models.", | |
).launch() |