panchadip commited on
Commit
d45bbd1
·
verified ·
1 Parent(s): f93596e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ # Initialize the text generation pipeline with a CPU device
6
+ generator = pipeline("text-generation", model="gpt2", device=torch.device('cpu'))
7
+
8
+ # Streamlit app layout
9
+ st.title("AI Story Generator")
10
+ st.write("Generate stories based on your custom prompt!")
11
+
12
+ # Input prompt for the user
13
+ prompt = st.text_input("Enter a story prompt:")
14
+
15
+ # Generate button to start the story generation
16
+ if st.button("Generate Story"):
17
+ if prompt:
18
+ try:
19
+ # Generate story based on the prompt
20
+ result = generator(prompt, max_length=100, num_return_sequences=1)
21
+
22
+ # Display the generated story
23
+ st.subheader("Generated Story:")
24
+ st.write(result[0]['generated_text'])
25
+
26
+ except Exception as e:
27
+ # Display any errors that occur during generation
28
+ st.error(f"An error occurred: {str(e)}")
29
+ else:
30
+ st.warning("Please enter a prompt to generate a story.")