suhaifLLM commited on
Commit
c58a629
·
verified ·
1 Parent(s): bca293e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # file: interactive_storytelling.py
2
+ import streamlit as st
3
+ from unsloth import FastLanguageModel
4
+ from transformers import TextStreamer
5
+ # import torch
6
+
7
+ # Initialize the model and tokenizer
8
+ max_seq_length = 2048
9
+ dtype = None # Auto-detection
10
+ load_in_4bit = True
11
+
12
+ # Load the pre-trained model
13
+ model, tokenizer = FastLanguageModel.from_pretrained(
14
+ model_name="suhaif/unsloth-llama-3-8b-4bit",
15
+ max_seq_length=max_seq_length,
16
+ dtype=dtype,
17
+ load_in_4bit=load_in_4bit,
18
+ )
19
+
20
+ FastLanguageModel.for_inference(model)
21
+
22
+ # Set up the Streamlit app
23
+ st.title("Interactive Storytelling Assistant")
24
+ st.write("Collaboratively write stories and get real-time suggestions!")
25
+
26
+ # Text input for the user prompt
27
+ user_input = st.text_area("Write the beginning of your story:", placeholder="Once upon a time...")
28
+
29
+ if st.button("Generate Suggestions"):
30
+ if user_input:
31
+ # Tokenize and send the input to the model
32
+ inputs = tokenizer([user_input], return_tensors="pt")
33
+
34
+ # Stream the generated output
35
+ text_streamer = TextStreamer(tokenizer)
36
+ st.write("Generating suggestions...")
37
+ generated_text = model.generate(**inputs, streamer=text_streamer, max_new_tokens=250)
38
+
39
+ # Display the generated text
40
+ st.subheader("Story Suggestions:")
41
+ st.write(generated_text)
42
+
43
+ # Feedback section for rating
44
+ rating = st.slider("Rate the suggestion:", 1, 5)
45
+ if rating:
46
+ st.success(f"Thank you for your feedback! You rated this {rating} stars.")
47
+
48
+ # Community sharing section
49
+ st.subheader("Community Stories")
50
+ st.write("Upload your story and share it with the community!")
51
+
52
+ uploaded_story = st.file_uploader("Upload your story (txt file)", type="txt")
53
+
54
+ if uploaded_story:
55
+ # Display uploaded story
56
+ story_text = uploaded_story.read().decode("utf-8")
57
+ st.text_area("Your Uploaded Story:", story_text, height=300)
58
+
59
+ # Display other community stories (could be expanded with database integration)
60
+ st.subheader("Community Stories Feedback Section")
61
+ st.write("In this section, you can provide critiques for other community stories.")