Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,96 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
26 |
-
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
demo.launch()
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from unsloth import FastLanguageModel
|
3 |
+
import torch
|
4 |
|
5 |
+
# Load the pre-trained language model and tokenizer
|
6 |
+
model_name = "suhaif/unsloth-llama-3-8b-4bit"
|
7 |
+
max_seq_length = 2048
|
8 |
+
dtype = None
|
9 |
+
load_in_4bit = True
|
10 |
|
11 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
12 |
+
model_name=model_name,
|
13 |
+
max_seq_length=max_seq_length,
|
14 |
+
dtype=dtype,
|
15 |
+
load_in_4bit=load_in_4bit
|
16 |
+
)
|
17 |
|
18 |
+
# Default instruction for generating the story
|
19 |
+
default_instruction = "You are a creative writer. Based on the given input, generate a well-structured story with an engaging plot, well-developed characters, and immersive details. Ensure the story has a clear beginning, middle, and end. Include dialogue and descriptions to bring the story to life. You can add twist to the story also"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
# Function to format the prompt
|
22 |
+
def format_prompt(input_text, instruction=default_instruction):
|
23 |
+
return f"{instruction}\n\nInput:\n{input_text}\n\nResponse:\n"
|
|
|
|
|
24 |
|
25 |
+
# Function to generate story from the model
|
26 |
+
def generate_story(user_input):
|
27 |
+
# Format the input
|
28 |
+
prompt = format_prompt(user_input)
|
29 |
+
inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
|
30 |
|
31 |
+
# Generate output from the model
|
32 |
+
outputs = model.generate(**inputs, max_new_tokens=500, use_cache=True)
|
33 |
+
|
34 |
+
# Decode and return the result
|
35 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
36 |
|
37 |
+
# Feedback mechanism (collects and stores feedback)
|
38 |
+
feedback_data = []
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
+
def submit_feedback(rating, feedback_text, story):
|
41 |
+
feedback_data.append({
|
42 |
+
"rating": rating,
|
43 |
+
"feedback_text": feedback_text,
|
44 |
+
"story": story
|
45 |
+
})
|
46 |
+
return "Thank you for your feedback!"
|
47 |
|
48 |
+
# Community engagement feature - to upload and share stories
|
49 |
+
shared_stories = []
|
50 |
|
51 |
+
def share_story(title, story_text):
|
52 |
+
shared_stories.append({"title": title, "story_text": story_text})
|
53 |
+
return f"Story '{title}' has been shared successfully!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
def display_stories():
|
56 |
+
return [(story['title'], story['story_text']) for story in shared_stories]
|
57 |
|
58 |
+
# Gradio interface
|
59 |
+
def storytelling_interface():
|
60 |
+
# User inputs
|
61 |
+
with gr.Blocks() as demo:
|
62 |
+
gr.Markdown("# Interactive Storytelling Assistant")
|
63 |
+
|
64 |
+
with gr.Row():
|
65 |
+
with gr.Column():
|
66 |
+
user_input = gr.Textbox(label="Enter your story prompt", placeholder="A young adventurer embarks on a journey to find a lost treasure...", lines=4)
|
67 |
+
generate_button = gr.Button("Generate Story")
|
68 |
+
|
69 |
+
story_output = gr.Textbox(label="Generated Story", placeholder="Generated story will appear here...", lines=10, interactive=False)
|
70 |
+
|
71 |
+
generate_button.click(fn=generate_story, inputs=user_input, outputs=story_output)
|
72 |
+
|
73 |
+
with gr.Column():
|
74 |
+
gr.Markdown("## Provide Feedback")
|
75 |
+
rating = gr.Slider(1, 5, step=1, label="Rate the story")
|
76 |
+
feedback_text = gr.Textbox(label="Feedback", placeholder="Provide any suggestions or comments...", lines=3)
|
77 |
+
submit_feedback_button = gr.Button("Submit Feedback")
|
78 |
+
submit_feedback_button.click(fn=submit_feedback, inputs=[rating, feedback_text, story_output], outputs=None)
|
79 |
+
|
80 |
+
with gr.Row():
|
81 |
+
gr.Markdown("## Share your Story")
|
82 |
+
title = gr.Textbox(label="Story Title", placeholder="Enter the title of your story")
|
83 |
+
story_text = gr.Textbox(label="Your Story", placeholder="Enter your full story here...", lines=8)
|
84 |
+
share_button = gr.Button("Share Story")
|
85 |
+
share_button.click(fn=share_story, inputs=[title, story_text], outputs=None)
|
86 |
+
|
87 |
+
with gr.Row():
|
88 |
+
gr.Markdown("## Browse Shared Stories")
|
89 |
+
stories_list = gr.Dropdown(display_stories, label="Select a story to read")
|
90 |
+
story_display = gr.Textbox(label="Story Content", lines=10, interactive=False)
|
91 |
+
stories_list.change(fn=lambda title: next(story['story_text'] for story in shared_stories if story['title'] == title), inputs=stories_list, outputs=story_display)
|
92 |
+
|
93 |
demo.launch()
|
94 |
+
|
95 |
+
# Start the storytelling interface
|
96 |
+
storytelling_interface()
|