File size: 5,504 Bytes
92ddc2e
 
d1b54ac
92ddc2e
d1b54ac
92ddc2e
 
f010e45
d1b54ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f0c27c
d1b54ac
 
f010e45
d1b54ac
f010e45
d1b54ac
 
 
92ddc2e
3f0c27c
f010e45
 
 
3f0c27c
 
 
d1b54ac
92ddc2e
3f0c27c
d1b54ac
f010e45
d1b54ac
 
f010e45
d1b54ac
 
f010e45
92ddc2e
d1b54ac
f010e45
d1b54ac
 
 
 
 
 
f010e45
 
 
 
d1b54ac
f010e45
 
d1b54ac
f010e45
d1b54ac
 
 
 
 
 
 
 
 
 
 
f010e45
d1b54ac
f010e45
 
 
 
d1b54ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f010e45
 
 
 
 
 
d1b54ac
92ddc2e
 
d1b54ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import gradio as gr
from huggingface_hub import InferenceClient
import random

# Initialize the inference client
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

# Story genres and themes
STORY_GENRES = [
    "fantasy", "sci-fi", "mystery", "horror", "historical", 
    "cyberpunk", "western", "post-apocalyptic", "steampunk"
]

def get_enhanced_system_prompt(genre=None):
    """Generate a detailed system prompt with optional genre specification"""
    selected_genre = genre or random.choice(STORY_GENRES)
    
    system_message = f"""You are an interactive storyteller creating an immersive {selected_genre} choose-your-own-adventure story.

For each response:
1. Provide vivid sensory descriptions of the scene, environment, and characters
2. Include meaningful dialogue or internal monologue that reveals character motivations
3. End with exactly 3 different possible actions or decisions, each offering a distinct path
4. Maintain consistent world-building and character development
5. Incorporate appropriate atmosphere and tone for a {selected_genre} setting
6. Remember previous choices to create a coherent narrative arc

Format your three options as:
- Option 1: [Complete sentence describing a possible action]
- Option 2: [Complete sentence describing a possible action]
- Option 3: [Complete sentence describing a possible action]

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."""
    
    return system_message

def respond(message, chat_history, genre=None):
    """Generate a response based on the current message and conversation history"""
    # Use system prompt
    system_message = get_enhanced_system_prompt(genre)
    
    # Initialize messages with system prompt
    messages = [{"role": "system", "content": system_message}]
    
    # Add history (limited to last 5 exchanges to prevent token overflow)
    memory_length = 5
    for user_msg, bot_msg in chat_history[-memory_length:]:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": bot_msg})
    
    # Add current message
    messages.append({"role": "user", "content": message})
    
    # Special handling for story initialization
    if len(chat_history) == 0 or message.lower() in ["start", "begin", "begin my adventure"]:
        messages.append({
            "role": "system", 
            "content": f"Begin a new {genre or random.choice(STORY_GENRES)} adventure with an intriguing opening scene."
        })
    
    # Generate response
    response = ""
    try:
        for chunk in client.chat_completion(
            messages,
            max_tokens=512,
            stream=True,
            temperature=0.7,
            top_p=0.95,
        ):
            token = chunk.choices[0].delta.content
            if token:  # Check if token is not None
                response += token
            yield chat_history + [[message, response]]
    except Exception as e:
        error_response = f"Story magic temporarily interrupted. Please try again. (Error: {str(e)})"
        yield chat_history + [[message, error_response]]

# Create interface with additional customization
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🔮 Interactive Story Adventure")
    gr.Markdown("Immerse yourself in an interactive story where your choices shape the narrative.")
    
    with gr.Row():
        with gr.Column(scale=3):
            chatbot = gr.Chatbot(
                height=500,
                bubble_full_width=False,
                show_copy_button=True,
                avatar_images=(None, "🧙"),
                label="Chatbot"
            )
            
            # Important: This properly initializes the chatbot with an empty list of message pairs
            state = gr.State([])
            
            msg = gr.Textbox(
                placeholder="Describe what you want to do next in the story...",
                container=False,
                scale=4,
            )
            
            with gr.Row():
                submit = gr.Button("Continue Story", variant="primary")
                clear = gr.Button("Start New Adventure")
                
        with gr.Column(scale=1):
            gr.Markdown("## Adventure Settings")
            genre = gr.Dropdown(
                choices=STORY_GENRES,
                label="Story Genre",
                info="Select a genre for your next adventure",
                value="fantasy"
            )
            
            examples = gr.Examples(
                examples=[
                    ["Begin my adventure"],
                    ["I explore the mysterious cave"],
                    ["I approach the merchant to ask about the rumors"],
                    ["I decide to climb the tower"],
                    ["I hide in the shadows and observe"]
                ],
                inputs=msg
            )
    
    # Fixed event handlers that maintain proper chat history format
    msg.submit(respond, [msg, chatbot, genre], [chatbot])
    submit.click(respond, [msg, chatbot, genre], [chatbot])
    
    # Properly reset the chatbot with an empty list to fix the tuple format error
    clear.click(lambda: [], None, chatbot, queue=False)
    clear.click(lambda: "", None, msg, queue=False)

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)