PeterPinetree's picture
Update app.py
3f0c27c verified
raw
history blame
1.91 kB
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)