Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
-
# from langchain.llms import OpenAI
|
3 |
-
from langchain_openai import OpenAI
|
4 |
-
from transformers import pipeline
|
5 |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
6 |
-
|
7 |
import os
|
8 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
9 |
|
10 |
-
|
11 |
-
# text_generation_model = pipeline("text-generation", model="openai-community/gpt2-large")
|
12 |
-
# text_generation_model = pipeline("text-generation", model="distilbert/distilgpt2")
|
13 |
|
14 |
# Load image captioning model
|
15 |
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
@@ -20,7 +13,6 @@ feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
|
|
20 |
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
|
21 |
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint)
|
22 |
|
23 |
-
|
24 |
def generate_story(image, theme, genre):
|
25 |
try:
|
26 |
# Preprocess the image
|
@@ -36,27 +28,28 @@ def generate_story(image, theme, genre):
|
|
36 |
# Generate story based on the caption
|
37 |
story_prompt = f"Write an interesting {theme} story in the {genre} genre. The story should be within 100 words about {caption_text}."
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
# story =
|
42 |
|
43 |
-
|
|
|
|
|
|
|
44 |
except Exception as e:
|
45 |
return f"An error occurred during inference: {str(e)}"
|
46 |
|
47 |
-
|
48 |
-
|
49 |
# Gradio interface
|
50 |
-
input_image = gr.Image(label="Select Image",type="pil")
|
51 |
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")
|
52 |
input_genre = gr.Dropdown(["Fantasy", "Science Fiction", "Poetry", "Mystery/Thriller", "Romance", "Historical Fiction", "Horror", "Adventure", "Drama", "Comedy"], label="Input Genre")
|
53 |
-
|
54 |
-
|
55 |
|
56 |
gr.Interface(
|
57 |
fn=generate_story,
|
58 |
inputs=[input_image, input_theme, input_genre],
|
59 |
-
outputs=output_text,
|
60 |
title="Image to Story Generator",
|
61 |
description="Generate a story from an image taking theme and genre as input. It leverages image captioning and text generation models.",
|
62 |
-
).launch()
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
from transformers import AutoTokenizer, ViTFeatureExtractor, VisionEncoderDecoderModel
|
|
|
3 |
import os
|
|
|
4 |
|
5 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
6 |
|
7 |
# Load image captioning model
|
8 |
encoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
|
|
|
13 |
tokenizer = AutoTokenizer.from_pretrained(decoder_checkpoint)
|
14 |
model = VisionEncoderDecoderModel.from_pretrained(model_checkpoint)
|
15 |
|
|
|
16 |
def generate_story(image, theme, genre):
|
17 |
try:
|
18 |
# Preprocess the image
|
|
|
28 |
# Generate story based on the caption
|
29 |
story_prompt = f"Write an interesting {theme} story in the {genre} genre. The story should be within 100 words about {caption_text}."
|
30 |
|
31 |
+
# Assume OpenAI class/functionality here
|
32 |
+
# llm = OpenAI(model_name="gpt-3.5-turbo-instruct", openai_api_key=openai_api_key)
|
33 |
+
# story = llm.invoke(story_prompt)
|
34 |
|
35 |
+
# Placeholder for story generation
|
36 |
+
story = "Generated story placeholder"
|
37 |
+
|
38 |
+
return caption_text, story
|
39 |
except Exception as e:
|
40 |
return f"An error occurred during inference: {str(e)}"
|
41 |
|
|
|
|
|
42 |
# Gradio interface
|
43 |
+
input_image = gr.Image(label="Select Image", type="pil")
|
44 |
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")
|
45 |
input_genre = gr.Dropdown(["Fantasy", "Science Fiction", "Poetry", "Mystery/Thriller", "Romance", "Historical Fiction", "Horror", "Adventure", "Drama", "Comedy"], label="Input Genre")
|
46 |
+
output_caption = gr.Textbox(label="Image Caption", lines=2)
|
47 |
+
output_text = gr.Textbox(label="Generated Story", lines=8)
|
48 |
|
49 |
gr.Interface(
|
50 |
fn=generate_story,
|
51 |
inputs=[input_image, input_theme, input_genre],
|
52 |
+
outputs=[output_caption, output_text],
|
53 |
title="Image to Story Generator",
|
54 |
description="Generate a story from an image taking theme and genre as input. It leverages image captioning and text generation models.",
|
55 |
+
).launch()
|