Spaces:
Sleeping
Sleeping
Download app.py from visionaryTEST/LearningNPC: direct link, hf CLI and curl.
- Browser
- Download file 889 Bytes
-
https://huggingface.co/spaces/visionaryTEST/LearningNPC/resolve/main/app.py
- Command line
-
hf download hf://spaces/visionaryTEST/LearningNPC/app.py
-
curl -L -o app.py https://huggingface.co/spaces/visionaryTEST/LearningNPC/resolve/main/app.py
889 Bytes
| from flask import Flask, request, jsonify | |
| import requests, os | |
| app = Flask(__name__) | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| MODEL = "mistralai/Mistral-7B-Instruct-v0.2" | |
| def chat(): | |
| data = request.json or {} | |
| user_msg = data.get("message", "") | |
| resp = requests.post( | |
| f"https://api-inference.huggingface.co/models/{MODEL}", | |
| headers={"Authorization": f"Bearer {HF_TOKEN}"}, | |
| json={"inputs": user_msg} | |
| ) | |
| try: | |
| model_output = resp.json() | |
| if isinstance(model_output, list) and "generated_text" in model_output[0]: | |
| return jsonify({"reply": model_output[0]["generated_text"]}) | |
| else: | |
| return jsonify({"reply": str(model_output)}) | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) |