Download app.py from Ynvers/Game_AI: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/spaces/Ynvers/Game_AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ynvers/Game_AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ynvers/Game_AI/resolve/main/app.py
2.58 kB
| import gradio as gr | |
| import os | |
| from helper import load_world, save_world, get_mistral_api_key, load_env | |
| from mistralai import Mistral | |
| demo = None | |
| client = Mistral(get_mistral_api_key()) | |
| world = load_world("Terra Bestia.json") | |
| kingdom = world['kingdoms']['Ventara'] | |
| town = kingdom['towns']['Aeris'] | |
| character = town['npcs']['Kael'] | |
| def start_game(main_loop, share=False): | |
| global demo | |
| if demo is not None: | |
| demo.close() | |
| demo = gr.ChatInterface( | |
| main_loop, | |
| chatbot=gr.Chatbot(height=250, placeholder="Type 'start game' to begin"), | |
| textbox=gr.Textbox(placeholder="What do you do next?", container=False, scale=7), | |
| title="AI RPG", | |
| # description="Ask Yes Man any question", | |
| theme="soft", | |
| examples=["Look around", "Continue the story"], | |
| cache_examples=False, | |
| #retry_btn="Retry", | |
| #undo_btn="Undo", | |
| #clear_btn="Clear", | |
| ) | |
| demo.launch(share=share, server_name="0.0.0.0") | |
| def run_action(message, history, game_state): | |
| if(message.strip().lower() == 'start game'): | |
| return game_state['start'] | |
| system_prompt = """ | |
| Vous êtes un maître du jeu IA. Votre travail consiste à écrire ce qui se passe ensuite dans le jeu d'aventure d'un joueur.\ | |
| Instructions : \ | |
| Vous ne devez écrire qu'une à trois phrases en réponse. \ | |
| Écrivez toujours à la deuxième personne du présent. \ | |
| Ex. (Vous regardez vers le nord et voyez...) | |
| """ | |
| world_info = f""" | |
| World: {game_state['world']} | |
| Kingdom: {game_state['kingdom']} | |
| Town: {game_state['town']} | |
| Your Character: {game_state['character']}""" | |
| messages = [ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": world_info} | |
| ] | |
| for action in history: | |
| messages.append({"role": "assistant", "content": action[0]}) | |
| messages.append({"role": "user", "content": action[1]}) | |
| try: | |
| model_output = client.chat.complete( | |
| model="mistral-large-latest", | |
| messages=messages | |
| ) | |
| result = model_output.choices[0].message.content | |
| except Exception as e: | |
| result = f"An error occurred: {str(e)}" | |
| return result | |
| game_state = { | |
| "world": world['description'], | |
| "kingdom": kingdom['description'], | |
| "town": town['description'], | |
| "character": character['description'], | |
| "start": world['start'] | |
| } | |
| def main_loop(message, history): | |
| return run_action(message, history, game_state) | |
| start_game(main_loop, True) |