Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,133 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
|
|
|
| 4 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
# Add history
|
| 21 |
-
for user_msg, bot_msg in history:
|
| 22 |
messages.append({"role": "user", "content": user_msg})
|
| 23 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 24 |
|
|
|
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
response = ""
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
import random
|
| 4 |
|
| 5 |
+
# Initialize the inference client
|
| 6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 7 |
|
| 8 |
+
# Story genres and themes for variety
|
| 9 |
+
STORY_GENRES = [
|
| 10 |
+
"fantasy", "sci-fi", "mystery", "horror", "historical",
|
| 11 |
+
"cyberpunk", "western", "post-apocalyptic", "steampunk"
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
# Define character archetypes for the assistant to use
|
| 15 |
+
CHARACTER_ARCHETYPES = [
|
| 16 |
+
"the reluctant hero", "the wise mentor", "the cunning rogue",
|
| 17 |
+
"the loyal companion", "the mysterious stranger", "the noble warrior"
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
def get_enhanced_system_prompt(genre=None):
|
| 21 |
+
"""Generate a detailed system prompt with optional genre specification"""
|
| 22 |
+
selected_genre = genre or random.choice(STORY_GENRES)
|
| 23 |
+
|
| 24 |
+
system_message = f"""You are an interactive storyteller creating an immersive {selected_genre} choose-your-own-adventure story.
|
| 25 |
+
|
| 26 |
+
For each response:
|
| 27 |
+
1. Provide vivid sensory descriptions of the scene, environment, and characters
|
| 28 |
+
2. Include meaningful dialogue or internal monologue that reveals character motivations
|
| 29 |
+
3. End with exactly 3 different possible actions or decisions, each offering a distinct path
|
| 30 |
+
4. Maintain consistent world-building and character development
|
| 31 |
+
5. Incorporate appropriate atmosphere and tone for a {selected_genre} setting
|
| 32 |
+
6. Remember previous choices to create a coherent narrative arc
|
| 33 |
+
|
| 34 |
+
Format your three options as:
|
| 35 |
+
- Option 1: [Complete sentence describing a possible action]
|
| 36 |
+
- Option 2: [Complete sentence describing a possible action]
|
| 37 |
+
- Option 3: [Complete sentence describing a possible action]
|
| 38 |
+
|
| 39 |
+
Keep responses engaging but concise (200-300 words maximum). If the user's input doesn't clearly indicate a choice, interpret their intent and move the story forward in the most logical direction."""
|
| 40 |
|
| 41 |
+
return system_message
|
| 42 |
+
|
| 43 |
+
def respond(message, history, genre=None, memory_length=5):
|
| 44 |
+
"""Generate a response based on the current message and conversation history"""
|
| 45 |
+
# Use a more detailed system prompt
|
| 46 |
+
system_message = get_enhanced_system_prompt(genre)
|
| 47 |
+
|
| 48 |
+
# Initialize messages with system prompt
|
| 49 |
messages = [{"role": "system", "content": system_message}]
|
| 50 |
|
| 51 |
+
# Add limited history to prevent token overflow (only use recent exchanges)
|
| 52 |
+
for user_msg, bot_msg in history[-memory_length:]:
|
| 53 |
messages.append({"role": "user", "content": user_msg})
|
| 54 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 55 |
|
| 56 |
+
# Add current message
|
| 57 |
messages.append({"role": "user", "content": message})
|
| 58 |
|
| 59 |
+
# Special handling for story initialization
|
| 60 |
+
if len(history) == 0 or message.lower() in ["start", "begin", "begin my adventure"]:
|
| 61 |
+
# Add a specific instruction for starting a new story
|
| 62 |
+
messages.append({
|
| 63 |
+
"role": "system",
|
| 64 |
+
"content": f"Begin a new {genre or random.choice(STORY_GENRES)} adventure with an intriguing opening scene. Introduce the protagonist without assuming too much about them, allowing the user to shape the character."
|
| 65 |
+
})
|
| 66 |
+
|
| 67 |
+
# Generate and stream response
|
| 68 |
response = ""
|
| 69 |
+
try:
|
| 70 |
+
for message in client.chat_completion(
|
| 71 |
+
messages,
|
| 72 |
+
max_tokens=512,
|
| 73 |
+
stream=True,
|
| 74 |
+
temperature=0.7,
|
| 75 |
+
top_p=0.95,
|
| 76 |
+
):
|
| 77 |
+
token = message.choices[0].delta.content
|
| 78 |
+
response += token
|
| 79 |
+
yield response
|
| 80 |
+
except Exception as e:
|
| 81 |
+
error_message = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
|
| 82 |
+
yield error_message
|
| 83 |
+
|
| 84 |
+
# Create interface with additional customization options
|
| 85 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 86 |
+
gr.Markdown("# 🔮 Interactive Story Adventure")
|
| 87 |
+
gr.Markdown("Immerse yourself in an interactive story where your choices shape the narrative.")
|
| 88 |
+
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column(scale=3):
|
| 91 |
+
chatbot = gr.Chatbot(
|
| 92 |
+
height=500,
|
| 93 |
+
bubble_full_width=False,
|
| 94 |
+
show_copy_button=True,
|
| 95 |
+
avatar_images=(None, "🧙"),
|
| 96 |
+
)
|
| 97 |
+
msg = gr.Textbox(
|
| 98 |
+
placeholder="Describe what you want to do next in the story...",
|
| 99 |
+
container=False,
|
| 100 |
+
scale=4,
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
submit = gr.Button("Continue Story", variant="primary")
|
| 105 |
+
clear = gr.Button("Start New Adventure")
|
| 106 |
+
|
| 107 |
+
with gr.Column(scale=1):
|
| 108 |
+
gr.Markdown("## Adventure Settings")
|
| 109 |
+
genre = gr.Dropdown(
|
| 110 |
+
choices=STORY_GENRES,
|
| 111 |
+
label="Story Genre",
|
| 112 |
+
info="Select a genre for your next adventure",
|
| 113 |
+
value="fantasy"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
examples = gr.Examples(
|
| 117 |
+
examples=[
|
| 118 |
+
["Begin my adventure"],
|
| 119 |
+
["I explore the mysterious cave"],
|
| 120 |
+
["I approach the merchant to ask about the rumors"],
|
| 121 |
+
["I decide to climb the tower"],
|
| 122 |
+
["I hide in the shadows and observe"]
|
| 123 |
+
],
|
| 124 |
+
inputs=msg
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
msg.submit(respond, [msg, chatbot, genre], chatbot)
|
| 128 |
+
submit.click(respond, [msg, chatbot, genre], chatbot)
|
| 129 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 130 |
+
clear.click(lambda: "", None, msg, queue=False)
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|