smtsead commited on
Commit
3928fc8
·
verified ·
1 Parent(s): 1510296

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -7
app.py CHANGED
@@ -4,10 +4,10 @@ from transformers import pipeline
4
  from gtts import gTTS
5
  import os
6
 
7
- # Function to convert image to text using Hugging Face's vit-gpt2-image-captioning model
8
  def img2text(url):
9
  """
10
- Converts an image to text using the nlpconnect/vit-gpt2-image-captioning model.
11
 
12
  Args:
13
  url (str): Path to the image file.
@@ -15,7 +15,7 @@ def img2text(url):
15
  Returns:
16
  str: Generated text caption from the image, without words like "illustration".
17
  """
18
- image_to_text_model = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
19
  text = image_to_text_model(url)[0]["generated_text"]
20
 
21
  # Remove unwanted words like "illustration"
@@ -28,7 +28,7 @@ def img2text(url):
28
  # Function to generate a kid-friendly story from the text caption
29
  def text2story(text):
30
  """
31
- Generates a kid-friendly story from the text caption using the aspis/gpt2-genre-story-generation model.
32
 
33
  Args:
34
  text (str): Text caption generated from the image.
@@ -40,15 +40,15 @@ def text2story(text):
40
  prompt = f"Write a kids story based on below scenario :{text}."
41
 
42
  # Load the text generation model
43
- story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
44
 
45
  # Generate the story
46
- story = story_generator(prompt, max_length=150, num_return_sequences=1, temperature=0.7)[0]["generated_text"]
47
 
48
  # Remove the prompt from the generated story
49
  story = story.replace(prompt, "").strip()
50
 
51
- # Ensure the story is within 95 words
52
  story_words = story.split()
53
  if len(story_words) > 100:
54
  story = " ".join(story_words[:100])
 
4
  from gtts import gTTS
5
  import os
6
 
7
+ # Function to convert image to text using Hugging Face's BLIP model
8
  def img2text(url):
9
  """
10
+ Converts an image to text using the Salesforce/blip-image-captioning-base model.
11
 
12
  Args:
13
  url (str): Path to the image file.
 
15
  Returns:
16
  str: Generated text caption from the image, without words like "illustration".
17
  """
18
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
19
  text = image_to_text_model(url)[0]["generated_text"]
20
 
21
  # Remove unwanted words like "illustration"
 
28
  # Function to generate a kid-friendly story from the text caption
29
  def text2story(text):
30
  """
31
+ Generates a kid-friendly story from the text caption using the pranavpsv/gpt2-genre-story-generator model.
32
 
33
  Args:
34
  text (str): Text caption generated from the image.
 
40
  prompt = f"Write a kids story based on below scenario :{text}."
41
 
42
  # Load the text generation model
43
+ story_generator = pipeline("text-generation", model="pranavpsv/gpt2-genre-story-generator")
44
 
45
  # Generate the story
46
+ story = story_generator(prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
47
 
48
  # Remove the prompt from the generated story
49
  story = story.replace(prompt, "").strip()
50
 
51
+ # Ensure the story is within 100 words
52
  story_words = story.split()
53
  if len(story_words) > 100:
54
  story = " ".join(story_words[:100])