Dendup's picture
Update app.py
03046c2 verified
raw
history blame
No virus
1.38 kB
import streamlit as st
from transformers import pipeline
import requests
# Load the model
story_generator = pipeline('text-generation', model='gpt2')
st.title('Interactive Short-Story Creator')
# Genre selection
genre = st.selectbox(
"Choose the genre of your story:",
('Fantasy', 'Sci-Fi', 'Mystery', 'Horror', 'Adventure', 'Romance')
)
# Text area for user input
user_input = st.text_area("Start your story here...", "Once upon a time")
if st.button('Generate Story Continuation'):
with st.spinner('AI is writing the story...'):
prompt = f"A {genre.lower()} story. {user_input}"
generated_story = story_generator(prompt, max_length=500)
story_text = generated_story[0]['generated_text']
st.text_area("Here's the continuation of your story:", story_text, height=250)
# Fetch an image related to the genre
response = requests.get(f"https://api.pexels.com/v1/search?query={genre.lower()}&per_page=1",
headers={"Authorization": "YOUR_API_KEY"})
if response.status_code == 200:
image_url = response.json()['photos'][0]['src']['original']
st.image(image_url, caption=f"Visualizing the {genre} genre")
else:
st.error("Failed to fetch image")
# Note: Replace 'YOUR_API_KEY' with your actual API key from Pexels or Unsplash.