Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import os
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
client = OpenAI(base_url="https://router.huggingface.co/v1",
|
| 7 |
+
api_key=os.environ["HF_TOKEN"])
|
| 8 |
+
MODEL = "Qwen/Qwen3-Next-80B-A3B-Instruct"
|
| 9 |
+
|
| 10 |
+
history = []
|
| 11 |
+
|
| 12 |
+
@app.post("/chat")
|
| 13 |
+
def chat():
|
| 14 |
+
user = request.json["message"]
|
| 15 |
+
history.append({"role": "user", "content": user})
|
| 16 |
+
rsp = client.chat.completions.create(model=MODEL, messages=history)
|
| 17 |
+
bot = rsp.choices[0].message.content
|
| 18 |
+
history.append({"role": "assistant", "content": bot})
|
| 19 |
+
return jsonify({"reply": bot})
|
| 20 |
+
|
| 21 |
+
if __name__ == "__main__":
|
| 22 |
+
app.run(debug=True)
|
| 23 |
+
|