-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
121 lines (100 loc) · 5.64 KB
/
app.py
File metadata and controls
121 lines (100 loc) · 5.64 KB
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
import json
from json import JSONDecodeError
import streamlit as st
from hugchat import hugchat
import prompts
import utils
# Streamlit page configuration
st.set_page_config(
page_title="TaleMancer",
page_icon="🔮",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'Get Help': 'https://www.extremelycoolapp.com/help',
'Report a bug': "https://github.com/KraszewskiK/TaleMancer/issues",
'About': '''## About TaleMancer
TaleMancer is an interactive RPG app that allows you to embark on captivating storytelling adventures. Shape the story through your choices and immerse yourself in a world of epic quests and mystical encounters.
With the power of a large language model, TaleMancer generates dynamic narratives that respond to your decisions. Every choice you make influences the plot, characters, and outcomes, giving you a personalized and engaging gameplay experience.
Whether you seek thrilling battles, mysterious puzzles, or deep character interactions, TaleMancer offers multiple story arcs and questlines for you to explore. Each playthrough offers a unique journey, with consequences and endings that reflect the paths you choose.
Get ready to unleash your imagination and become the master of your own tale in TaleMancer!
Start your adventure now and let the stories unfold!
---
**Note:** TaleMancer is developed using Streamlit, an open-source framework for building interactive web applications with Python. It utilizes the power of a large language model to deliver an immersive RPG experience.
'''
}
)
# Load cookies
cookies = json.loads(st.secrets['cookies'])
# Create the chatbot instance
if 'chatbot' not in st.session_state:
st.session_state['chatbot'] = hugchat.ChatBot(cookies=cookies)
st.session_state['chat_id'] = st.session_state['chatbot'].get_conversation_list()[-1]
st.session_state['conversation'] = {st.session_state['chat_id']: {'prompts': [], 'responses': []}}
chatbot = st.session_state['chatbot']
chat_id = st.session_state['chat_id']
# Page design
with st.sidebar:
if st.session_state['conversation'][chat_id]['responses']:
if st.button('Start a new story'):
# Create a new conversation
st.session_state['chat_id'] = chatbot.new_conversation()
chatbot.change_conversation(st.session_state['chat_id'])
st.session_state['conversation'][st.session_state['chat_id']] = {'prompts': [], 'responses': []}
st.title('TaleMancer RPG storyteller🎮📚🔮')
st.markdown('''
## Disclaimer
- This is a beta version of the app. You will probably experience some bugs.
- Loading times while *Writing the story* are rather long.
- After you click one of the options and the next part loads, it is advised to click on the previously chosen \
option again. It should make previous buttons disappear.
## About
This is an LLM-powered app built using:
- [Streamlit](https://streamlit.io/)
- [HugChat](https://github.com/Soulter/hugging-chat-api)
- [OpenAssistant/oasst-sft-6-llama-30b-xor](https://huggingface.co/OpenAssistant/oasst-sft-6-llama-30b-xor) \
Large Language Model
💡 Note: No API key required!
''')
st.write('Made by [KraszewskiK](https://github.com/KraszewskiK)')
st.write("## Welcome to the TaleMancer app! Get ready to begin your adventure!")
# get model's response, put prompt and response in session_state
def get_response(prompt, choice='', is_first=False):
with st.spinner('Writing the story'):
st.session_state['conversation'][chat_id]['prompts'].append(choice)
try:
story, choices = utils.generate_response(chatbot, prompt, chat_id, cookies[0], is_first)
except JSONDecodeError:
st.warning("Internal error. Try to click on your choice one more time.")
st.session_state['conversation'][chat_id]['responses'].append((story, choices))
# if add_id_message['status'] != 200 or preserve_context_message['status'] != 200:
# st.error(f'{add_id_message["message"]} \n {preserve_context_message["message"]}')
if not st.session_state['conversation'][chat_id]['prompts']:
start_story = st.button('Start your story')
if start_story:
get_response(prompts.INITIAL_PROMPT, is_first=True)
# write the previous responses and prompts
story_so_far = ''
for i, response in enumerate(st.session_state['conversation'][chat_id]['responses']):
st.write(response[0])
story_so_far = '\n'.join([story_so_far, response[0], 'Choices:', '; '.join(response[1])])
# write stories and chosen options until the last response
if i != len(st.session_state['conversation'][chat_id]['responses']) - 1:
st.write(st.session_state['conversation'][chat_id]['prompts'][i + 1])
story_so_far = '\n'.join(
[story_so_far, ' '.join(
['I chose:', st.session_state['conversation'][chat_id]['prompts'][i + 1]])
]
)
st.divider()
else: # for the last response create buttons for each choice
choice_button_1 = st.button(response[1][0])
choice_button_2 = st.button(response[1][1])
choice_button_3 = st.button(response[1][2])
# after clicking the button create a prompt based on the choice
if choice_button_1:
get_response('\n'.join([story_so_far, ' '.join(['I chose:', response[1][0]])]), response[1][0])
if choice_button_2:
get_response('\n'.join([story_so_far, ' '.join(['I chose:', response[1][1]])]), response[1][1])
if choice_button_3:
get_response('\n'.join([story_so_far, ' '.join(['I chose:', response[1][2]])]), response[1][2])