apratim24 commited on
Commit
fdd924a
·
verified ·
1 Parent(s): 51fe236

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -1,9 +1,16 @@
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"
9
  decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
@@ -13,6 +20,7 @@ feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_checkpoint)
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,23 +36,23 @@ def generate_story(image, theme, genre):
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,
@@ -52,4 +60,4 @@ gr.Interface(
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()
 
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
+ # Load text generation model
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"
16
  decoder_checkpoint = "nlpconnect/vit-gpt2-image-captioning"
 
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
  # 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
+ llm = OpenAI(model_name="gpt-3.5-turbo-instruct", openai_api_key=openai_api_key)
40
+ story = llm.invoke(story_prompt)
41
+ # story = text_generation_model(story_prompt, max_length=150)[0]["generated_text"]
 
 
 
42
 
43
  return caption_text, story
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
  output_caption = gr.Textbox(label="Image Caption", lines=2)
54
+ output_text = gr.Textbox(label="Generated Story",lines=8)
55
+
56
 
57
  gr.Interface(
58
  fn=generate_story,
 
60
  outputs=[output_caption, output_text],
61
  title="Image to Story Generator",
62
  description="Generate a story from an image taking theme and genre as input. It leverages image captioning and text generation models.",
63
+ ).launch()