Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -579,31 +579,124 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 579 |
|
| 580 |
demo.launch()'''
|
| 581 |
|
| 582 |
-
import
|
| 583 |
-
|
| 584 |
-
API_TOKEN = "Sakshi"
|
| 585 |
-
API_URL = f"https://api-inference.huggingface.co/models/TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 586 |
-
HEADERS = {"Authorization": f"Bearer {API_TOKEN}"}
|
| 587 |
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
prompt = f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 598 |
|
| 599 |
-
|
| 600 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 601 |
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
|
|
|
|
|
|
|
| 609 |
|
|
|
|
| 579 |
|
| 580 |
demo.launch()'''
|
| 581 |
|
| 582 |
+
import gradio as gr
|
| 583 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 584 |
|
| 585 |
+
# Load a lightweight text generation model from Hugging Face
|
| 586 |
+
generator = pipeline("text-generation", model="distilgpt2", max_length=100)
|
| 587 |
+
|
| 588 |
+
# Initialize story state
|
| 589 |
+
story_history = []
|
| 590 |
+
|
| 591 |
+
def generate_story_segment(prompt, user_choice=None):
|
| 592 |
+
"""Generate a story segment and three choices based on the prompt."""
|
| 593 |
+
if user_choice:
|
| 594 |
+
prompt = f"{prompt} {user_choice}"
|
| 595 |
+
|
| 596 |
+
# Generate a story segment
|
| 597 |
+
story_output = generator(prompt, max_length=100, num_return_sequences=1, do_sample=True, temperature=0.7)[0]["generated_text"]
|
| 598 |
+
|
| 599 |
+
# Generate three possible choices for the next step
|
| 600 |
+
choice_prompt = f"{story_output} What happens next? Provide three short options."
|
| 601 |
+
choices_output = generator(choice_prompt, max_length=150, num_return_sequences=3, do_sample=True, temperature=0.9)
|
| 602 |
+
|
| 603 |
+
# Extract and clean choices
|
| 604 |
+
choices = [choice["generated_text"].split("Provide three short options.")[-1].strip() for choice in choices_output]
|
| 605 |
+
cleaned_choices = []
|
| 606 |
+
for choice in choices:
|
| 607 |
+
# Simple heuristic to extract short options (could be improved with regex or parsing)
|
| 608 |
+
if len(choice) > 0:
|
| 609 |
+
cleaned_choices.append(choice[:50] + "..." if len(choice) > 50 else choice)
|
| 610 |
+
else:
|
| 611 |
+
cleaned_choices.append("Something unexpected happens...")
|
| 612 |
+
|
| 613 |
+
# Ensure we always have 3 choices
|
| 614 |
+
while len(cleaned_choices) < 3:
|
| 615 |
+
cleaned_choices.append("A new adventure begins...")
|
| 616 |
+
|
| 617 |
+
return story_output, cleaned_choices[:3]
|
| 618 |
+
|
| 619 |
+
def start_story(initial_prompt):
|
| 620 |
+
"""Start the story with the user's initial prompt."""
|
| 621 |
+
global story_history
|
| 622 |
+
story_history = [initial_prompt]
|
| 623 |
+
segment, choices = generate_story_segment(initial_prompt)
|
| 624 |
+
story_history.append(segment)
|
| 625 |
+
return "\n".join(story_history), choices
|
| 626 |
+
|
| 627 |
+
def continue_story(choice):
|
| 628 |
+
"""Continue the story based on the user's choice."""
|
| 629 |
+
global story_history
|
| 630 |
+
segment, choices = generate_story_segment(story_history[-1], choice)
|
| 631 |
+
story_history.append(f"You chose: {choice}")
|
| 632 |
+
story_history.append(segment)
|
| 633 |
+
return "\n".join(story_history), choices
|
| 634 |
+
|
| 635 |
+
def reset_story():
|
| 636 |
+
"""Reset the story to start fresh."""
|
| 637 |
+
global story_history
|
| 638 |
+
story_history = []
|
| 639 |
+
return "Story reset. Enter a new prompt to begin!", ["", "", ""]
|
| 640 |
+
|
| 641 |
+
# Gradio interface
|
| 642 |
+
with gr.Blocks(title="Interactive Story Generator") as demo:
|
| 643 |
+
gr.Markdown("# Interactive Story Generator")
|
| 644 |
+
gr.Markdown("Start your story by entering a prompt below. Then, choose options to guide the adventure!")
|
| 645 |
+
|
| 646 |
+
# Initial prompt input
|
| 647 |
+
with gr.Row():
|
| 648 |
+
prompt_input = gr.Textbox(label="Enter your story prompt (e.g., 'A knight in a dark forest')", placeholder="Type here...")
|
| 649 |
+
start_button = gr.Button("Start Story")
|
| 650 |
+
|
| 651 |
+
# Story display and choice selection
|
| 652 |
+
story_output = gr.Textbox(label="Your Story", lines=10, interactive=False)
|
| 653 |
+
choice_buttons = [
|
| 654 |
+
gr.Button(value="", visible=True),
|
| 655 |
+
gr.Button(value="", visible=True),
|
| 656 |
+
gr.Button(value="", visible=True)
|
| 657 |
+
]
|
| 658 |
+
|
| 659 |
+
# Reset button
|
| 660 |
+
reset_button = gr.Button("Reset Story")
|
| 661 |
+
|
| 662 |
+
# Logic to start the story
|
| 663 |
+
def update_choices(story, choices):
|
| 664 |
+
return story, [
|
| 665 |
+
gr.Button.update(value=choices[0], visible=True),
|
| 666 |
+
gr.Button.update(value=choices[1], visible=True),
|
| 667 |
+
gr.Button.update(value=choices[2], visible=True)
|
| 668 |
+
]
|
| 669 |
+
|
| 670 |
+
start_button.click(
|
| 671 |
+
fn=start_story,
|
| 672 |
+
inputs=prompt_input,
|
| 673 |
+
outputs=[story_output, choice_buttons[0], choice_buttons[1], choice_buttons[2]],
|
| 674 |
+
_js="() => {return [document.querySelector('input').value]}"
|
| 675 |
+
).then(
|
| 676 |
+
fn=update_choices,
|
| 677 |
+
inputs=[story_output, gr.State(value=["", "", ""])],
|
| 678 |
+
outputs=[story_output, choice_buttons[0], choice_buttons[1], choice_buttons[2]]
|
| 679 |
+
)
|
| 680 |
|
| 681 |
+
# Logic to continue the story with choices
|
| 682 |
+
for i, button in enumerate(choice_buttons):
|
| 683 |
+
button.click(
|
| 684 |
+
fn=continue_story,
|
| 685 |
+
inputs=gr.State(value=button.value),
|
| 686 |
+
outputs=[story_output, choice_buttons[0], choice_buttons[1], choice_buttons[2]],
|
| 687 |
+
_js=f"() => ['{button.value}']"
|
| 688 |
+
).then(
|
| 689 |
+
fn=update_choices,
|
| 690 |
+
inputs=[story_output, gr.State(value=["", "", ""])],
|
| 691 |
+
outputs=[story_output, choice_buttons[0], choice_buttons[1], choice_buttons[2]]
|
| 692 |
+
)
|
| 693 |
|
| 694 |
+
# Reset the story
|
| 695 |
+
reset_button.click(
|
| 696 |
+
fn=reset_story,
|
| 697 |
+
outputs=[story_output, choice_buttons[0], choice_buttons[1], choice_buttons[2]]
|
| 698 |
+
)
|
|
|
|
| 699 |
|
| 700 |
+
# Launch the app
|
| 701 |
+
demo.launch()
|
| 702 |
|