Spaces:
Sleeping
Sleeping
| DEEPSEEK_API_KEY = "sk-2fb0ae0c0925471e928e24544d469695" # 請填入您的API金鑰 | |
| # 文字冒險遊戲模板輸入系統 - Gradio版本 | |
| # Text Adventure Game Template Input System - Gradio Version | |
| # 文字冒險遊戲模板輸入系統 - 改進版 | |
| # Text Adventure Game Template Input System - Enhanced Version | |
| import json | |
| from datetime import datetime | |
| import gradio as gr | |
| import requests | |
| # DeepSeek API 設定 | |
| DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions" | |
| class GameTemplateManager: | |
| def __init__(self): | |
| self.templates = { | |
| "world": {}, | |
| "characters": {}, | |
| "plot": {}, | |
| "generated_content": {}, | |
| "game_context": "", # 用於儲存遊戲背景資訊,供互動使用 | |
| } | |
| self.conversation_history = [] # 儲存對話歷史 | |
| def call_deepseek(self, prompt: str, conversation_mode=False) -> str: | |
| """呼叫DeepSeek API或返回模擬內容""" | |
| if not DEEPSEEK_API_KEY or DEEPSEEK_API_KEY == "your_deepseek_api_key_here": | |
| return self.get_mock_content(prompt, conversation_mode) | |
| headers = { | |
| "Authorization": f"Bearer {DEEPSEEK_API_KEY}", | |
| "Content-Type": "application/json", | |
| } | |
| messages = [{"role": "user", "content": prompt}] | |
| # 如果是對話模式,加入歷史記錄 | |
| if conversation_mode and self.conversation_history: | |
| messages = self.conversation_history + [{"role": "user", "content": prompt}] | |
| data = { | |
| "model": "deepseek-chat", | |
| "messages": messages, | |
| "max_tokens": 600, | |
| "temperature": 0.8, | |
| } | |
| try: | |
| response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data) | |
| if response.status_code == 200: | |
| return response.json()["choices"][0]["message"]["content"] | |
| else: | |
| return self.get_mock_content(prompt, conversation_mode) | |
| except Exception as e: | |
| return self.get_mock_content(prompt, conversation_mode) | |
| def get_mock_content(self, prompt: str, conversation_mode=False) -> str: | |
| """提供模擬AI回應""" | |
| if conversation_mode: | |
| # 互動模式的回應 | |
| if "看看" in prompt or "觀察" in prompt: | |
| return "你仔細觀察四周,發現了一些有趣的細節。遠處傳來神秘的聲音,似乎在呼喚著什麼。你感覺到這個地方充滿了未知的秘密,等待著你去探索。" | |
| elif "前進" in prompt or "走" in prompt: | |
| return "你勇敢地向前走去,腳步聲在空曠的地方迴盪。突然,你看到前方出現了一道奇特的光芒,似乎在指引著什麼方向。你的冒險即將迎來新的轉機!" | |
| elif "說話" in prompt or "交談" in prompt: | |
| return "一個溫和的聲音回應了你:『歡迎來到這個神奇的世界,年輕的冒險者。你的到來並非偶然,這裡需要像你這樣勇敢的人。準備好迎接挑戰了嗎?』" | |
| else: | |
| return "你的行動引起了周圍環境的微妙變化。微風輕撫,樹葉沙沙作響,似乎整個世界都在回應你的存在。接下來會發生什麼呢?" | |
| elif "世界描述" in prompt or "世界背景" in prompt: | |
| return """這是一個充滿魔法與奇蹟的神秘世界。高聳入雲的水晶塔散發著柔和的光芒,古老的森林中住著會說話的動物們。清澈的河流從雲端流下,形成美麗的彩虹瀑布。在這個世界裡,善良的心靈能夠喚醒沉睡的魔法力量,每一次冒險都充滿了驚喜與感動。星星在白天也閃閃發光,為旅行者指引方向。""" | |
| elif "角色對話" in prompt or "角色介紹" in prompt: | |
| return """【主角自我介紹】 | |
| 「大家好!我是小光,一個充滿勇氣的年輕冒險者。雖然我還在學習中,但我相信只要有勇氣和善良的心,就能克服任何困難!我最喜歡幫助別人,也喜歡探索未知的地方。」 | |
| 【反派威脅台詞】 | |
| 「哼哼哼!你們這些小鬼根本不知道我的力量有多強大!我要讓整個世界都籠罩在黑暗中,只有我才能統治一切!」 | |
| 【主角勇敢宣言】 | |
| 「不管前方有多危險,我都不會退縮!為了保護大家,為了守護這個美好的世界,我一定要勇敢前進!光明永遠會戰勝黑暗!」""" | |
| elif "故事場景" in prompt or "情節描述" in prompt: | |
| return """【冒險開始場景】 | |
| 清晨的陽光透過窗戶灑進房間,小光被一陣急促的敲門聲驚醒。門外傳來村長焦急的聲音,原來村子裡的守護神獸突然失蹤了!神獸的消失讓整個村莊籠罩在不安的氣氛中。 | |
| 【遇到挑戰場景】 | |
| 前方出現了一座巨大的迷宮,石牆高聳入雲,入口處刻著古老的文字。突然,迷宮中傳來奇怪的吼聲,讓人心跳加速。空氣中瀰漫著神秘的魔法氣息,這會是什麼挑戰呢? | |
| 【最終對決場景】 | |
| 在神秘塔的頂層,小光終於面對了黑暗魔王。雷電在空中閃爍,大地在顫抖。魔王的邪惡笑聲迴盪在整個塔樓,這場決定世界命運的戰鬥即將開始!誰會是最後的勝利者呢?""" | |
| else: | |
| return "AI正在為你創作精彩的故事內容,請稍候..." | |
| # 創建模板管理器 | |
| template_manager = GameTemplateManager() | |
| # 世界背景模板函數 | |
| def save_world_template( | |
| world_name, | |
| world_type, | |
| main_location, | |
| special_item, | |
| world_crisis, | |
| world_mood, | |
| world_rule, | |
| ): | |
| """儲存世界背景模板""" | |
| world_template = { | |
| "世界名稱": world_name or "神秘世界", | |
| "世界類型": world_type or "奇幻魔法", | |
| "主要地點": main_location or "魔法森林", | |
| "特殊物品": special_item or "神奇寶石", | |
| "世界危機": world_crisis or "黑暗力量威脅", | |
| "世界氛圍": world_mood or "神秘冒險", | |
| "世界規則": world_rule or "善良的心能感應魔法", | |
| } | |
| template_manager.templates["world"] = world_template | |
| # 格式化顯示模板內容 | |
| template_display = f""" | |
| 🌍 世界背景模板已儲存! | |
| 📝 你的世界設定: | |
| • 世界名稱:{world_template["世界名稱"]} | |
| • 世界類型:{world_template["世界類型"]} | |
| • 主要地點:{world_template["主要地點"]} | |
| • 特殊物品:{world_template["特殊物品"]} | |
| • 世界危機:{world_template["世界危機"]} | |
| • 世界氛圍:{world_template["世界氛圍"]} | |
| • 世界規則:{world_template["世界規則"]} | |
| 💡 提示:如果你沒有填寫某些欄位,AI會自動為你生成合適的內容! | |
| """ | |
| return "✅ 世界模板儲存成功!", template_display | |
| # 角色模板函數 | |
| def save_character_template( | |
| hero_name, | |
| hero_age, | |
| hero_job, | |
| hero_ability, | |
| hero_personality, | |
| hero_background, | |
| hero_goal, | |
| villain_name, | |
| villain_type, | |
| villain_motivation, | |
| villain_weakness, | |
| villain_appearance, | |
| ): | |
| """儲存角色模板""" | |
| # 為空白欄位提供預設值 | |
| character_template = { | |
| "主角": { | |
| "名字": hero_name or "小勇", | |
| "年齡": hero_age or "11-13歲", | |
| "職業": hero_job or "學生冒險者", | |
| "特殊能力": hero_ability or "勇氣和智慧", | |
| "個性特徵": hero_personality or ["勇敢", "善良"], | |
| "背景故事": hero_background or "一個平凡但有夢想的孩子", | |
| "目標願望": hero_goal or "成為真正的英雄", | |
| }, | |
| "反派": { | |
| "名字": villain_name or "暗影大師", | |
| "類型": villain_type or "邪惡科學家", | |
| "動機原因": villain_motivation or "想要統治世界", | |
| "弱點": villain_weakness or "過度自信", | |
| "外貌特徵": villain_appearance or "神秘的黑色斗篷", | |
| }, | |
| } | |
| template_manager.templates["characters"] = character_template | |
| template_display = f""" | |
| 👥 角色模板已儲存! | |
| 🦸 主角設定: | |
| • 名字:{character_template["主角"]["名字"]} | |
| • 年齡:{character_template["主角"]["年齡"]} | |
| • 職業:{character_template["主角"]["職業"]} | |
| • 特殊能力:{character_template["主角"]["特殊能力"]} | |
| • 個性特徵:{character_template["主角"]["個性特徵"]} | |
| • 背景故事:{character_template["主角"]["背景故事"]} | |
| • 目標願望:{character_template["主角"]["目標願望"]} | |
| 🦹 反派設定: | |
| • 名字:{character_template["反派"]["名字"]} | |
| • 類型:{character_template["反派"]["類型"]} | |
| • 動機原因:{character_template["反派"]["動機原因"]} | |
| • 弱點:{character_template["反派"]["弱點"]} | |
| • 外貌特徵:{character_template["反派"]["外貌特徵"]} | |
| 💡 提示:未填寫的欄位已自動設定為預設值,AI會據此生成內容! | |
| """ | |
| return "✅ 角色模板儲存成功!", template_display | |
| # 情節模板函數 | |
| def save_plot_template( | |
| story_theme, | |
| opening_scene, | |
| inciting_incident, | |
| first_obstacle, | |
| midpoint_twist, | |
| climax_battle, | |
| resolution_ending, | |
| ): | |
| """儲存情節模板""" | |
| plot_template = { | |
| "故事主題": story_theme or "友誼與勇氣的力量", | |
| "開場場景": opening_scene or "平靜的村莊突然出現異象", | |
| "引發事件": inciting_incident or "重要的東西被偷走了", | |
| "第一障礙": first_obstacle or "通往目標的路被阻擋", | |
| "中點轉折": midpoint_twist or "發現意想不到的真相", | |
| "高潮戰鬥": climax_battle or "與反派的最終對決", | |
| "結局解決": resolution_ending or "世界恢復和平,英雄凱旋", | |
| } | |
| template_manager.templates["plot"] = plot_template | |
| template_display = f""" | |
| 📖 情節模板已儲存! | |
| 📝 你的故事結構: | |
| • 故事主題:{plot_template["故事主題"]} | |
| • 開場場景:{plot_template["開場場景"]} | |
| • 引發事件:{plot_template["引發事件"]} | |
| • 第一障礙:{plot_template["第一障礙"]} | |
| • 中點轉折:{plot_template["中點轉折"]} | |
| • 高潮戰鬥:{plot_template["高潮戰鬥"]} | |
| • 結局解決:{plot_template["結局解決"]} | |
| 💡 提示:未填寫的情節要素已設定為經典冒險故事結構! | |
| """ | |
| return "✅ 情節模板儲存成功!", template_display | |
| # 生成AI內容函數 | |
| def generate_ai_content(): | |
| """使用模板內容生成AI增強版故事""" | |
| templates = template_manager.templates | |
| # 如果沒有任何模板,創建基本設定 | |
| if not templates["world"]: | |
| templates["world"] = { | |
| "世界名稱": "奇幻王國", | |
| "世界類型": "奇幻魔法", | |
| "主要地點": "魔法森林", | |
| "特殊物品": "智慧水晶", | |
| "世界危機": "黑暗勢力入侵", | |
| "世界氛圍": "神秘冒險", | |
| "世界規則": "善良的心能喚醒魔法", | |
| } | |
| if not templates["characters"]: | |
| templates["characters"] = { | |
| "主角": { | |
| "名字": "小光", | |
| "年齡": "12歲", | |
| "職業": "見習魔法師", | |
| "特殊能力": "光明魔法", | |
| "個性特徵": ["勇敢", "善良", "聰明"], | |
| "背景故事": "來自普通家庭的孩子,偶然發現自己的魔法天賦", | |
| "目標願望": "成為保護世界的偉大魔法師", | |
| }, | |
| "反派": { | |
| "名字": "暗影領主", | |
| "類型": "墮落的古代法師", | |
| "動機原因": "被黑暗力量腐蝕,想要將世界拖入永恆黑暗", | |
| "弱點": "害怕純真的光明之心", | |
| "外貌特徵": "身穿破爛的黑色長袍,眼中燃燒著邪惡的紅光", | |
| }, | |
| } | |
| if not templates["plot"]: | |
| templates["plot"] = { | |
| "故事主題": "光明戰勝黑暗,友誼給予力量", | |
| "開場場景": "平和的村莊突然被黑霧籠罩", | |
| "引發事件": "村莊的守護之光被暗影領主偷走", | |
| "第一障礙": "必須通過危險的黑暗森林", | |
| "中點轉折": "發現暗影領主曾經也是善良的法師", | |
| "高潮戰鬥": "在光明神殿中與暗影領主決戰", | |
| "結局解決": "用友誼和愛心淨化了暗影領主,世界重獲光明", | |
| } | |
| # 生成世界描述 | |
| world_prompt = f""" | |
| 請基於以下設定,創作一個生動詳細的遊戲世界描述,適合小學生閱讀: | |
| 世界名稱:{templates["world"]["世界名稱"]} | |
| 世界類型:{templates["world"]["世界類型"]} | |
| 主要地點:{templates["world"]["主要地點"]} | |
| 特殊物品:{templates["world"]["特殊物品"]} | |
| 世界危機:{templates["world"]["世界危機"]} | |
| 世界氛圍:{templates["world"]["世界氛圍"]} | |
| 世界規則:{templates["world"]["世界規則"]} | |
| 請用300字左右創作世界背景描述,語言要優美生動,能激發小學生的想像力,讓他們彷彿身臨其境。 | |
| """ | |
| world_description = template_manager.call_deepseek(world_prompt) | |
| # 生成角色介紹 | |
| character_prompt = f""" | |
| 請基於以下角色設定,創作生動的角色介紹和對話: | |
| 主角:{templates["characters"]["主角"]["名字"]} | |
| - 年齡:{templates["characters"]["主角"]["年齡"]} | |
| - 職業:{templates["characters"]["主角"]["職業"]} | |
| - 特殊能力:{templates["characters"]["主角"]["特殊能力"]} | |
| - 個性:{templates["characters"]["主角"]["個性特徵"]} | |
| - 背景:{templates["characters"]["主角"]["背景故事"]} | |
| - 目標:{templates["characters"]["主角"]["目標願望"]} | |
| 反派:{templates["characters"]["反派"]["名字"]} | |
| - 類型:{templates["characters"]["反派"]["類型"]} | |
| - 動機:{templates["characters"]["反派"]["動機原因"]} | |
| - 弱點:{templates["characters"]["反派"]["弱點"]} | |
| - 外貌:{templates["characters"]["反派"]["外貌特徵"]} | |
| 請創作: | |
| 1. 主角的詳細介紹和個性描述(100字) | |
| 2. 反派的詳細介紹和威脅感描述(100字) | |
| 3. 主角的勇敢宣言(50字) | |
| 4. 反派的邪惡宣言(50字) | |
| 語言要符合角色年齡和個性,生動有趣。 | |
| """ | |
| character_content = template_manager.call_deepseek(character_prompt) | |
| # 生成故事開場 | |
| story_prompt = f""" | |
| 請基於以下設定,創作故事的精彩開場,讓玩家能夠立即投入冒險: | |
| 故事主題:{templates["plot"]["故事主題"]} | |
| 開場場景:{templates["plot"]["開場場景"]} | |
| 引發事件:{templates["plot"]["引發事件"]} | |
| 世界背景:{templates["world"]["世界名稱"]}的{templates["world"]["主要地點"]} | |
| 主角:{templates["characters"]["主角"]["名字"]} | |
| 請創作一個約200字的故事開場,要: | |
| 1. 立即吸引讀者注意 | |
| 2. 清楚介紹情境 | |
| 3. 讓玩家想要開始冒險 | |
| 4. 語言適合小學生,充滿想像力 | |
| 最後以一個問題結尾,邀請玩家開始互動。 | |
| """ | |
| story_opening = template_manager.call_deepseek(story_prompt) | |
| # 整合所有生成內容並準備遊戲背景 | |
| generated_content = f""" | |
| 🌟 你的專屬冒險遊戲已生成完成! | |
| 🌍 【世界背景】 | |
| {world_description} | |
| 👥 【角色介紹】 | |
| {character_content} | |
| 📖 【冒險開始】 | |
| {story_opening} | |
| 🎮 現在你可以開始與這個世界互動了!在下方輸入你想要做的事情,AI會即時回應你的行動。 | |
| 💡 互動提示: | |
| • 你可以說:「我想探索周圍」、「和某人說話」、「使用魔法」等 | |
| • 盡情發揮想像力,這個世界會回應你的每一個行動! | |
| """ | |
| # 儲存遊戲背景供互動使用 | |
| template_manager.game_context = f""" | |
| 遊戲設定: | |
| 世界:{templates["world"]["世界名稱"]}({templates["world"]["世界類型"]}) | |
| 地點:{templates["world"]["主要地點"]} | |
| 主角:{templates["characters"]["主角"]["名字"]}({templates["characters"]["主角"]["職業"]}) | |
| 主角能力:{templates["characters"]["主角"]["特殊能力"]} | |
| 主角個性:{templates["characters"]["主角"]["個性特徵"]} | |
| 反派:{templates["characters"]["反派"]["名字"]} | |
| 世界危機:{templates["world"]["世界危機"]} | |
| 特殊物品:{templates["world"]["特殊物品"]} | |
| 世界規則:{templates["world"]["世界規則"]} | |
| 故事背景:{story_opening} | |
| """ | |
| # 初始化對話歷史 | |
| template_manager.conversation_history = [ | |
| { | |
| "role": "system", | |
| "content": f"你是一個文字冒險遊戲的AI導師,正在為小學生提供互動式冒險體驗。{template_manager.game_context} 請根據玩家的行動,提供生動有趣、適合小學生的回應,每次回應約100-150字。保持故事的連貫性和趣味性。", | |
| } | |
| ] | |
| # 儲存生成內容 | |
| template_manager.templates["generated_content"] = { | |
| "world_description": world_description, | |
| "character_content": character_content, | |
| "story_opening": story_opening, | |
| "complete_content": generated_content, | |
| } | |
| return "✅ AI冒險世界生成完成!現在可以開始互動了!", generated_content | |
| # 互動功能函數 | |
| def start_adventure_interaction(user_input, chat_history): | |
| """處理玩家與遊戲世界的互動""" | |
| if not template_manager.game_context: | |
| return chat_history, "請先生成AI內容後再開始互動!" | |
| if not user_input.strip(): | |
| return chat_history, "請輸入你想要在冒險中做的事情!" | |
| # 構建互動提示 | |
| interaction_prompt = f""" | |
| 作為文字冒險遊戲的AI導師,玩家在{template_manager.templates["world"]["世界名稱"]}中說:"{user_input}" | |
| 請根據以下背景提供回應: | |
| {template_manager.game_context} | |
| 要求: | |
| 1. 回應要生動有趣,適合小學生 | |
| 2. 保持故事連貫性 | |
| 3. 鼓勵進一步探索 | |
| 4. 約100-150字 | |
| 5. 可以適時引入新的發現或角色 | |
| 6. 保持積極正面的氛圍 | |
| 請以第二人稱回應玩家的行動。 | |
| """ | |
| # 獲取AI回應 | |
| ai_response = template_manager.call_deepseek( | |
| interaction_prompt, conversation_mode=True | |
| ) | |
| # 更新對話歷史 | |
| template_manager.conversation_history.append( | |
| {"role": "user", "content": user_input} | |
| ) | |
| template_manager.conversation_history.append( | |
| {"role": "assistant", "content": ai_response} | |
| ) | |
| # 限制對話歷史長度,避免過長 | |
| if len(template_manager.conversation_history) > 10: | |
| template_manager.conversation_history = template_manager.conversation_history[ | |
| -8: | |
| ] | |
| # 更新聊天記錄 | |
| chat_history.append([user_input, ai_response]) | |
| return chat_history, "" | |
| def clear_chat(): | |
| """清除聊天記錄""" | |
| template_manager.conversation_history = [ | |
| { | |
| "role": "system", | |
| "content": f"你是一個文字冒險遊戲的AI導師,正在為小學生提供互動式冒險體驗。{template_manager.game_context} 請根據玩家的行動,提供生動有趣、適合小學生的回應。", | |
| } | |
| ] | |
| return [] | |
| # 匯出模板函數 | |
| def export_templates(): | |
| """匯出完整的模板內容""" | |
| templates = template_manager.templates | |
| if not templates["world"] and not templates["characters"]: | |
| return "❌ 請先完成至少一個模板!", "" | |
| export_content = f""" | |
| 📋 完整遊戲模板匯出 | |
| {"=" * 50} | |
| 創作時間:{datetime.now().strftime("%Y年%m月%d日 %H:%M")} | |
| 🌍 世界背景模板: | |
| """ | |
| # 世界模板 | |
| if templates["world"]: | |
| for key, value in templates["world"].items(): | |
| export_content += f"• {key}:{value}\n" | |
| # 角色模板 | |
| if templates["characters"]: | |
| export_content += f"\n👥 角色模板:\n" | |
| if "主角" in templates["characters"]: | |
| export_content += "🦸 主角設定:\n" | |
| for key, value in templates["characters"]["主角"].items(): | |
| export_content += f"• {key}:{value}\n" | |
| if "反派" in templates["characters"]: | |
| export_content += "\n🦹 反派設定:\n" | |
| for key, value in templates["characters"]["反派"].items(): | |
| export_content += f"• {key}:{value}\n" | |
| # 情節模板 | |
| if templates["plot"]: | |
| export_content += f"\n📖 情節模板:\n" | |
| for key, value in templates["plot"].items(): | |
| export_content += f"• {key}:{value}\n" | |
| # 生成內容 | |
| if templates["generated_content"]: | |
| export_content += f"\n🤖 AI生成內容:\n" | |
| export_content += templates["generated_content"].get("complete_content", "") | |
| export_content += f""" | |
| 🎊 你的冒險遊戲創作完成! | |
| 現在你可以繼續與這個世界互動,探索更多可能性! | |
| """ | |
| return "✅ 模板匯出成功!", export_content | |
| # 創建Gradio介面 | |
| def create_template_interface(): | |
| with gr.Blocks(title="文字冒險遊戲創作器", theme=gr.themes.Soft()) as interface: | |
| gr.Markdown(""" | |
| # 🎮 文字冒險遊戲創作器 | |
| ## 創造你的專屬冒險世界,並與AI一起探索! | |
| 歡迎來到神奇的遊戲創作世界!填寫模板創造你的冒險故事,然後與AI生成的世界進行即時互動! | |
| """) | |
| with gr.Tabs(): | |
| # 第一個標籤:世界設定 | |
| with gr.TabItem("🌍 第一步:創建世界"): | |
| gr.Markdown(""" | |
| ### 設計你的遊戲世界 | |
| 想像一個全新的世界吧!你可以填寫詳細資訊,也可以留空讓AI為你生成。 | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| world_name = gr.Textbox( | |
| label="🏰 世界名稱", | |
| placeholder="例如:彩虹王國、星際聯盟、魔法學院(可留空)", | |
| info="給你的世界起一個酷炫的名字", | |
| ) | |
| world_type = gr.Dropdown( | |
| label="🎭 世界類型", | |
| choices=[ | |
| "", | |
| "奇幻魔法", | |
| "科幻未來", | |
| "現代都市", | |
| "古代神話", | |
| "童話世界", | |
| "武俠江湖", | |
| "末日廢土", | |
| "海洋世界", | |
| ], | |
| value="", | |
| info="選擇世界的風格類型", | |
| ) | |
| main_location = gr.Textbox( | |
| label="📍 主要冒險地點", | |
| placeholder="例如:漂浮的魔法城堡、地下機器人工廠(可留空)", | |
| info="冒險主要發生在哪裡?", | |
| ) | |
| special_item = gr.Textbox( | |
| label="✨ 特殊物品或力量", | |
| placeholder="例如:會唱歌的魔法寶石、時間旅行裝置(可留空)", | |
| info="這個世界有什麼神奇的東西?", | |
| ) | |
| with gr.Column(): | |
| world_crisis = gr.Textbox( | |
| label="⚠️ 世界面臨的危機", | |
| placeholder="例如:邪惡巫師要偷走所有顏色(可留空)", | |
| info="什麼威脅讓這個世界需要英雄?", | |
| ) | |
| world_mood = gr.Dropdown( | |
| label="🎨 世界氛圍", | |
| choices=[ | |
| "", | |
| "溫馨友善", | |
| "神秘冒險", | |
| "緊張刺激", | |
| "幽默搞笑", | |
| "夢幻浪漫", | |
| "英勇史詩", | |
| ], | |
| value="", | |
| info="你希望這個世界給人什麼感覺?", | |
| ) | |
| world_rule = gr.Textbox( | |
| label="📜 世界特殊規則", | |
| placeholder="例如:只有善良的人能看見魔法(可留空)", | |
| info="這個世界有什麼特別的規則?", | |
| ) | |
| save_world_btn = gr.Button( | |
| "💾 儲存世界設定", variant="primary", size="lg" | |
| ) | |
| world_status = gr.Textbox(label="儲存狀態", interactive=False) | |
| world_template_display = gr.Textbox( | |
| label="你的世界設定", lines=8, interactive=False | |
| ) | |
| save_world_btn.click( | |
| save_world_template, | |
| inputs=[ | |
| world_name, | |
| world_type, | |
| main_location, | |
| special_item, | |
| world_crisis, | |
| world_mood, | |
| world_rule, | |
| ], | |
| outputs=[world_status, world_template_display], | |
| ) | |
| # 第二個標籤:角色設定 | |
| with gr.TabItem("👥 第二步:創建角色"): | |
| gr.Markdown(""" | |
| ### 創造你的遊戲角色 | |
| 設計有趣的主角和反派!可以填寫詳細資訊,也可以留空讓AI創造。 | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| gr.Markdown("#### 🦸 主角設定") | |
| hero_name = gr.Textbox( | |
| label="👤 主角名字", | |
| placeholder="例如:小光、艾拉、麥克斯(可留空)", | |
| info="你的主角叫什麼名字?", | |
| ) | |
| hero_age = gr.Dropdown( | |
| label="🎂 主角年齡", | |
| choices=[ | |
| "", | |
| "8-10歲", | |
| "11-13歲", | |
| "14-16歲", | |
| "17-19歲", | |
| "20歲以上", | |
| "神秘年齡", | |
| ], | |
| value="", | |
| ) | |
| hero_job = gr.Dropdown( | |
| label="💼 主角職業/身份", | |
| choices=[ | |
| "", | |
| "學生冒險者", | |
| "年輕法師", | |
| "機器人駕駛員", | |
| "海洋探險家", | |
| "時間旅行者", | |
| "動物朋友", | |
| "發明家", | |
| "運動員", | |
| ], | |
| value="", | |
| ) | |
| hero_ability = gr.Textbox( | |
| label="⚡ 特殊能力", | |
| placeholder="例如:能與動物對話、超強記憶力(可留空)", | |
| info="主角有什麼特別的才能?", | |
| ) | |
| hero_personality = gr.CheckboxGroup( | |
| label="🌟 個性特徵(可多選)", | |
| choices=[ | |
| "勇敢", | |
| "聰明", | |
| "善良", | |
| "幽默", | |
| "好奇", | |
| "堅強", | |
| "樂觀", | |
| "有責任感", | |
| ], | |
| value=[], | |
| ) | |
| hero_background = gr.Textbox( | |
| label="📚 背景故事", | |
| placeholder="例如:從小在圖書館長大(可留空)", | |
| info="主角的過去有什麼經歷?", | |
| lines=2, | |
| ) | |
| hero_goal = gr.Textbox( | |
| label="🎯 目標願望", | |
| placeholder="例如:成為最棒的冒險者(可留空)", | |
| info="主角最想達成什麼?", | |
| ) | |
| with gr.Column(): | |
| gr.Markdown("#### 🦹 反派設定(可選)") | |
| villain_name = gr.Textbox( | |
| label="👤 反派名字", | |
| placeholder="例如:暗影博士、機械女王(可留空)", | |
| info="你的反派叫什麼?", | |
| ) | |
| villain_type = gr.Dropdown( | |
| label="🎭 反派類型", | |
| choices=[ | |
| "", | |
| "邪惡科學家", | |
| "墮落法師", | |
| "機器統治者", | |
| "嫉妒的貴族", | |
| "被誤解的怪物", | |
| "貪婪的商人", | |
| "控制狂國王", | |
| ], | |
| value="", | |
| ) | |
| villain_motivation = gr.Textbox( | |
| label="💭 動機原因", | |
| placeholder="例如:小時候被人嘲笑(可留空)", | |
| info="反派為什麼要做壞事?", | |
| lines=2, | |
| ) | |
| villain_weakness = gr.Textbox( | |
| label="🔍 弱點", | |
| placeholder="例如:害怕蜘蛛、過度自信(可留空)", | |
| info="反派有什麼弱點?", | |
| ) | |
| villain_appearance = gr.Textbox( | |
| label="👁️ 外貌特徵", | |
| placeholder="例如:戴著奇怪的眼鏡(可留空)", | |
| info="反派長什麼樣子?", | |
| ) | |
| save_character_btn = gr.Button( | |
| "💾 儲存角色設定", variant="primary", size="lg" | |
| ) | |
| character_status = gr.Textbox(label="儲存狀態", interactive=False) | |
| character_template_display = gr.Textbox( | |
| label="你的角色設定", lines=12, interactive=False | |
| ) | |
| save_character_btn.click( | |
| save_character_template, | |
| inputs=[ | |
| hero_name, | |
| hero_age, | |
| hero_job, | |
| hero_ability, | |
| hero_personality, | |
| hero_background, | |
| hero_goal, | |
| villain_name, | |
| villain_type, | |
| villain_motivation, | |
| villain_weakness, | |
| villain_appearance, | |
| ], | |
| outputs=[character_status, character_template_display], | |
| ) | |
| # 第三個標籤:情節設定 | |
| with gr.TabItem("📖 第三步:設計情節(可選)"): | |
| gr.Markdown(""" | |
| ### 設計你的故事情節 | |
| 想想看故事如何發展?也可以留空讓AI為你創造驚喜! | |
| """) | |
| story_theme = gr.Textbox( | |
| label="🌟 故事主題", | |
| placeholder="例如:友誼的力量、勇氣戰勝恐懼(可留空)", | |
| info="你想通過這個故事傳達什麼?", | |
| ) | |
| with gr.Row(): | |
| with gr.Column(): | |
| opening_scene = gr.Textbox( | |
| label="🌅 開場場景", | |
| placeholder="例如:平靜的村莊突然出現奇怪現象(可留空)", | |
| info="故事如何開始?", | |
| lines=2, | |
| ) | |
| inciting_incident = gr.Textbox( | |
| label="⚡ 引發事件", | |
| placeholder="例如:最好的朋友突然失蹤(可留空)", | |
| info="什麼讓主角開始冒險?", | |
| lines=2, | |
| ) | |
| first_obstacle = gr.Textbox( | |
| label="🚧 第一個障礙", | |
| placeholder="例如:通往目標的路被阻擋(可留空)", | |
| info="主角遇到什麼困難?", | |
| lines=2, | |
| ) | |
| with gr.Column(): | |
| midpoint_twist = gr.Textbox( | |
| label="🔄 中點轉折", | |
| placeholder="例如:發現反派其實被控制(可留空)", | |
| info="故事有什麼意外發展?", | |
| lines=2, | |
| ) | |
| climax_battle = gr.Textbox( | |
| label="⚔️ 高潮戰鬥", | |
| placeholder="例如:在魔法塔與反派決戰(可留空)", | |
| info="最激動的對決是什麼?", | |
| lines=2, | |
| ) | |
| resolution_ending = gr.Textbox( | |
| label="🎊 結局解決", | |
| placeholder="例如:世界恢復和平(可留空)", | |
| info="故事如何結束?", | |
| lines=2, | |
| ) | |
| save_plot_btn = gr.Button( | |
| "💾 儲存情節設定", variant="primary", size="lg" | |
| ) | |
| plot_status = gr.Textbox(label="儲存狀態", interactive=False) | |
| plot_template_display = gr.Textbox( | |
| label="你的情節設定", lines=10, interactive=False | |
| ) | |
| save_plot_btn.click( | |
| save_plot_template, | |
| inputs=[ | |
| story_theme, | |
| opening_scene, | |
| inciting_incident, | |
| first_obstacle, | |
| midpoint_twist, | |
| climax_battle, | |
| resolution_ending, | |
| ], | |
| outputs=[plot_status, plot_template_display], | |
| ) | |
| # 第四個標籤:AI內容生成 | |
| with gr.TabItem("🤖 第四步:生成冒險世界"): | |
| gr.Markdown(""" | |
| ### 讓AI創造你的完整冒險世界 | |
| 基於你的設定(或AI自動生成),創造一個完整的可互動世界! | |
| """) | |
| gr.Markdown(""" | |
| 💡 **提示:** | |
| - 即使你沒有填寫任何模板,AI也會為你創造一個精彩的世界 | |
| - 你填寫得越詳細,生成的世界就越符合你的想像 | |
| - 生成完成後就可以開始與世界互動了! | |
| """) | |
| generate_ai_btn = gr.Button( | |
| "🚀 生成我的冒險世界", variant="primary", size="lg" | |
| ) | |
| ai_status = gr.Textbox(label="生成狀態", interactive=False) | |
| ai_content_display = gr.Textbox( | |
| label="你的專屬冒險世界", lines=15, interactive=False | |
| ) | |
| generate_ai_btn.click( | |
| generate_ai_content, outputs=[ai_status, ai_content_display] | |
| ) | |
| # 第五個標籤:互動冒險 | |
| with gr.TabItem("🎮 第五步:開始冒險!"): | |
| gr.Markdown(""" | |
| ### 🌟 與你的冒險世界互動 | |
| 現在你可以與AI創造的世界進行即時互動!輸入你想做的事情,AI會回應你的每個行動。 | |
| """) | |
| with gr.Row(): | |
| with gr.Column(scale=2): | |
| chatbot = gr.Chatbot( | |
| label="🎭 冒險對話", height=400, show_label=True | |
| ) | |
| with gr.Row(): | |
| user_input = gr.Textbox( | |
| label="", | |
| placeholder="輸入你想在冒險中做的事情...(例如:探索周圍、和村民說話、使用魔法等)", | |
| scale=4, | |
| ) | |
| submit_btn = gr.Button( | |
| "🎯 行動", variant="primary", scale=1 | |
| ) | |
| clear_btn = gr.Button("🔄 重新開始冒險", variant="secondary") | |
| with gr.Column(scale=1): | |
| gr.Markdown(""" | |
| ### 💡 互動提示 | |
| **你可以嘗試:** | |
| - 🚶 移動:「向前走」「進入森林」 | |
| - 👀 觀察:「看看周圍」「仔細觀察」 | |
| - 💬 對話:「和某人說話」「詢問訊息」 | |
| - ⚡ 行動:「使用魔法」「攀爬樹木」 | |
| - 🎒 道具:「尋找物品」「使用工具」 | |
| **記住:** | |
| - 發揮想像力! | |
| - AI會根據你的行動創造精彩情節 | |
| - 每次冒險都是獨一無二的 | |
| - 勇敢探索,享受冒險! | |
| """) | |
| interaction_status = gr.Textbox( | |
| label="冒險狀態", | |
| value="準備開始冒險!先生成你的世界,然後就可以開始互動了!", | |
| interactive=False, | |
| ) | |
| # 設定互動功能 | |
| def handle_interaction(user_msg, history): | |
| return start_adventure_interaction(user_msg, history) | |
| submit_btn.click( | |
| handle_interaction, | |
| inputs=[user_input, chatbot], | |
| outputs=[chatbot, interaction_status], | |
| ).then( | |
| lambda: "", # 清空輸入框 | |
| outputs=[user_input], | |
| ) | |
| user_input.submit( | |
| handle_interaction, | |
| inputs=[user_input, chatbot], | |
| outputs=[chatbot, interaction_status], | |
| ).then( | |
| lambda: "", # 清空輸入框 | |
| outputs=[user_input], | |
| ) | |
| clear_btn.click(clear_chat, outputs=[chatbot]).then( | |
| lambda: "冒險重新開始!", outputs=[interaction_status] | |
| ) | |
| # 第六個標籤:匯出作品 | |
| with gr.TabItem("📋 匯出作品"): | |
| gr.Markdown(""" | |
| ### 保存你的創作 | |
| 將你的遊戲設定和AI生成的內容匯出,可以保存或分享! | |
| """) | |
| export_btn = gr.Button("📥 匯出完整作品", variant="primary", size="lg") | |
| export_status = gr.Textbox(label="匯出狀態", interactive=False) | |
| export_content_display = gr.Textbox( | |
| label="完整作品內容", lines=20, interactive=False | |
| ) | |
| export_btn.click( | |
| export_templates, outputs=[export_status, export_content_display] | |
| ) | |
| # 頁尾提示 | |
| gr.Markdown(""" | |
| --- | |
| ### 🎊 創作小貼士 | |
| **📝 模板填寫:** | |
| - 可以詳細填寫每個欄位,也可以留空讓AI創造驚喜 | |
| - 你的創意想法會讓故事更獨特 | |
| - 不要害怕嘗試奇怪有趣的設定 | |
| **🤖 AI互動:** | |
| - AI會根據你的設定創造一致的世界 | |
| - 互動時可以嘗試任何你想做的事情 | |
| - AI會記住之前的對話,保持故事連貫 | |
| **🎮 冒險建議:** | |
| - 勇敢探索,不要害怕失敗 | |
| - 與不同角色交談,了解更多故事 | |
| - 使用你角色的特殊能力 | |
| - 享受每一個意想不到的情節轉折 | |
| **🌟 記住:每個人都是天生的故事創作者!** | |
| """) | |
| return interface | |
| # 啟動介面 | |
| if __name__ == "__main__": | |
| interface = create_template_interface() | |
| interface.launch( | |
| share=True, inbrowser=True, server_name="0.0.0.0", server_port=7860 | |
| ) | |