Joey04243 / app.py
Joey889's picture
Upload 4 files
4d2a9c8 verified
from flask import Flask, render_template, request
import requests
app = Flask(__name__)
HF_MODEL = "HuggingFaceH4/zephyr-7b-beta"
API_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
headers = {"Accept": "application/json"}
@app.route("/", methods=["GET", "POST"])
def index():
story = ""
image = ""
if request.method == "POST":
action = request.form.get("action")
if action == "使用魔法":
payload = {
"inputs": "你是一位幻想世界的敘述者。請描述角色施展魔法後的劇情,最多 80 字,語氣神秘、有趣。",
"parameters": {
"max_new_tokens": 60,
"temperature": 0.9,
"do_sample": True,
"return_full_text": False
}
}
try:
response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
response.raise_for_status()
generated = response.json()[0]["generated_text"].strip()
story = f"你施展魔法後的結果是:「{generated}」"
except Exception as e:
story = f"⚠️ Hugging Face 模型錯誤:{str(e)}"
elif action == "前進":
story = "你往前邁出一步,前方是一片壯麗的景色。"
image = "/static/landscape.jpg"
elif action == "觀察":
story = "你仔細觀察四周,這裡是一個充滿共創氛圍的領域。"
image = "/static/collaboration.jpg"
return render_template("index.html", story=story, image=image)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)