Spaces:
Running
Running
Update api.py
Browse files
api.py
CHANGED
@@ -7,31 +7,39 @@ CORS(app, resources={r"/*": {
|
|
7 |
"origins": "*",
|
8 |
"allow_headers": "*",
|
9 |
"methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
10 |
-
"
|
11 |
}})
|
12 |
|
13 |
-
def chat_with_model(query: str, model: str):
|
14 |
-
try:
|
15 |
-
results = DuckAI().chat(query, model=model)
|
16 |
-
return jsonify({"results": results})
|
17 |
-
except Exception as e:
|
18 |
-
return jsonify({"error": str(e)}), 500
|
19 |
-
|
20 |
@app.route("/chat/", methods=["GET"])
|
21 |
def chat():
|
22 |
query = request.args.get("query")
|
23 |
if not query:
|
24 |
return jsonify({"error": "Query parameter is required"}), 400
|
25 |
-
|
|
|
26 |
try:
|
27 |
-
results =
|
28 |
return jsonify({"results": results})
|
29 |
-
except Exception as
|
|
|
30 |
try:
|
31 |
-
results =
|
32 |
return jsonify({"results": results})
|
33 |
-
except Exception as
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
if __name__ == "__main__":
|
37 |
-
app.run(host="localhost", port=3000)
|
|
|
7 |
"origins": "*",
|
8 |
"allow_headers": "*",
|
9 |
"methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
10 |
+
"supports_credentials": True
|
11 |
}})
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
@app.route("/chat/", methods=["GET"])
|
14 |
def chat():
|
15 |
query = request.args.get("query")
|
16 |
if not query:
|
17 |
return jsonify({"error": "Query parameter is required"}), 400
|
18 |
+
|
19 |
+
duck = DuckAI() # create one DuckAI instance
|
20 |
try:
|
21 |
+
results = duck.chat(query, model='gpt-4o-mini')
|
22 |
return jsonify({"results": results})
|
23 |
+
except Exception as e1:
|
24 |
+
print(f"Primary model (gpt-4o-mini) failed: {e1}")
|
25 |
try:
|
26 |
+
results = duck.chat(query, model='claude-3-haiku')
|
27 |
return jsonify({"results": results})
|
28 |
+
except Exception as e2:
|
29 |
+
print(f"Fallback model (claude-3-haiku) also failed: {e2}")
|
30 |
+
return jsonify({
|
31 |
+
"error": "Both models failed",
|
32 |
+
"primary_error": str(e1),
|
33 |
+
"fallback_error": str(e2)
|
34 |
+
}), 500
|
35 |
+
|
36 |
+
def chat_with_model(query: str, model: str):
|
37 |
+
try:
|
38 |
+
duck = DuckAI()
|
39 |
+
results = duck.chat(query, model=model)
|
40 |
+
return jsonify({"results": results})
|
41 |
+
except Exception as e:
|
42 |
+
return jsonify({"error": str(e)}), 500
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
+
app.run(host="localhost", port=3000, debug=True)
|