fanta / app.py
fantaxy's picture
Create app.py
042cdb0 verified
raw
history blame
4.92 kB
# -*- 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()