File size: 1,910 Bytes
92ddc2e
 
 
 
 
3f0c27c
 
 
 
 
 
 
 
 
 
 
 
92ddc2e
3f0c27c
 
 
 
 
 
92ddc2e
3f0c27c
92ddc2e
 
 
3f0c27c
92ddc2e
3f0c27c
 
92ddc2e
 
 
 
 
3f0c27c
92ddc2e
 
3f0c27c
 
 
 
 
 
 
92ddc2e
3f0c27c
92ddc2e
 
 
3f0c27c
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
import gradio as gr
from huggingface_hub import InferenceClient

client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")

def respond(message, history):
    system_message = """You are an interactive storyteller creating an immersive choose-your-own-adventure story. 
    For each response:
    1. Provide vivid descriptions of the scene and characters
    2. Include meaningful dialogue or internal monologue
    3. End with 3 different possible actions or decisions, phrased as complete sentences
    Example endings:
    - You could try climbing the ancient tree to get a better view
    - Perhaps investigating the strange sound from the cave would reveal something
    - Maybe approaching the mysterious stranger to ask for directions is wise
    Keep responses engaging but concise."""
    
    messages = [{"role": "system", "content": system_message}]
    
    # Add history and current message
    for user_msg, bot_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": bot_msg})
    
    messages.append({"role": "user", "content": message})
    
    response = ""
    for message in client.chat_completion(
        messages,
        max_tokens=512,
        stream=True,
        temperature=0.7,
        top_p=0.95,
    ):
        token = message.choices[0].delta.content
        response += token
        yield response

# Create Gradio interface
demo = gr.ChatInterface(
    respond,
    title="Interactive Story Adventure",
    description="Describe what you want to do next in the story using natural language...",
    examples=[
        "Begin my adventure",
        "I want to explore the mysterious cave",
        "Let's try talking to the merchant",
        "I decide to climb the tower"
    ],
    theme=gr.themes.Soft()
)

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