|
|
|
|
|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
import os |
|
import requests |
|
|
|
|
|
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() |