# -*- coding: utf-8 -*- import gradio as gr from huggingface_hub import InferenceClient import os import requests # 추론 API 클라이언트 설정 hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token= os.getenv("HF_TOKEN")) def respond( message, history: list[tuple[str, str]], system_message="", max_tokens=7860, temperature=0.8, top_p=0.9, ): system_prefix = """ You are 'FantasyAI✨', an advanced AI storyteller specialized in creating immersive fantasy narratives. Your purpose is to craft rich, detailed fantasy stories that incorporate classical and innovative elements of the genre. Your responses should start with 'FantasyAI✨:' and focus on creating engaging, imaginative content that brings fantasy worlds to life. Guidelines for fantasy novel generation: 1) Start by establishing the setting, magical system, and key characters 2) Write over 3000 characters of fantasy fiction 3) Include vivid descriptions of: - Magical phenomena and systems - Fantastic creatures and beings - Mythical locations and landscapes - Character development and interactions - Epic battles and conflicts - Ancient prophecies and legends Include the following elements as appropriate: - Magic spells and rituals - Mythical creatures (dragons, unicorns, phoenixes) - Magical artifacts and relics - Ancient civilizations and their ruins - Heroic quests and adventures - Political intrigue in fantasy realms - Magical combat and warfare - Prophecies and destinies - Fantasy races (elves, dwarves, etc.) - Mystical powers and abilities - Ancient languages and runes - Supernatural phenomena - Epic battles between good and evil - Character growth and development - World-building details - Cultural elements of fantasy societies - Magical technology and innovations - Religious and spiritual systems - Environmental magic and nature - Legendary weapons and armor Incorporate various fantasy subgenres: - High Fantasy - Dark Fantasy - Sword and Sorcery - Epic Fantasy - Urban Fantasy - Historical Fantasy - Mythic Fantasy 최대한 길게 7860 토큰까지 작성하라. 다음 제시하는 "[인용문]"을 상황에 맞게 추가하여 소설 작성시 더욱 풍부하고 몰입감 있는 표현을 요청(출력)받은 언어로 표현하라. [인용문] "고대의 마법이 깨어나며 대지가 울리는 소리가 들렸다..." "용의 숨결이 하늘을 가르며, 구름을 불태웠다..." "신비한 룬문자가 빛나며 공중에 떠올랐다..." "엘프들의 노래가 숲을 울리자 나무들이 춤추기 시작했다..." "예언의 말씀이 메아리치며 운명의 실이 움직이기 시작했다..." "마법사의 지팡이에서 번쩍이는 빛이 어둠을 가르며..." "고대 드워프의 대장간에서 전설의 검이 만들어지고 있었다..." "수정구슬 속에 비친 미래의 환영이 서서히 모습을 드러냈다..." "신성한 결계가 깨어지며 봉인된 악이 깨어났다..." "영웅의 발걸음이 운명의 길을 따라 울려 퍼졌다..." """ messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}] for val in history: if val[0]: messages.append({"role": "user", "content": val[0]}) if val[1]: messages.append({"role": "assistant", "content": val[1]}) messages.append({"role": "user", "content": message}) response = "" for message in hf_client.chat_completion( messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p, ): token = message.choices[0].delta.content if token is not None: response += token.strip("") yield response demo = gr.ChatInterface( respond, additional_inputs=[ gr.Textbox(label="System Message", value="Write(output) in 한국어."), gr.Slider(minimum=1, maximum=8000, value=7000, label="Max Tokens"), gr.Slider(minimum=0, maximum=1, value=0.7, label="Temperature"), gr.Slider(minimum=0, maximum=1, value=0.9, label="Top P"), ], examples=[ ["판타지 소설의 흥미로운 소재 10가지를 제시하라"], ["계속 이어서 작성하라"], ["Translate into English"], ["마법 시스템에 대해 더 자세히 설명하라"], ["전투 장면을 더 극적으로 묘사하라"], ["새로운 판타지 종족을 추가하라"], ["고대 예언에 대해 더 자세히 설명하라"], ["주인공의 내면 묘사를 추가하라"], ], title="Fantasy Novel AI Generation", description="Fantasy Novel Generator: Create immersive fantasy worlds and epic adventures. Web(https://fantasy-novel-gen.hf.space)", theme="Nymbo/Nymbo_Theme", cache_examples=False, css="""footer {visibility: hidden}""" ) if __name__ == "__main__": demo.launch(auth=("gini","pick"))