Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,63 @@
|
|
1 |
-
import requests
|
2 |
from flask import Flask, request, jsonify
|
|
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
@app.route('/chat', methods=['POST'])
|
10 |
-
def chat():
|
11 |
try:
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
if not
|
16 |
-
return jsonify({"error": "
|
17 |
-
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
return (
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
except Exception as e:
|
37 |
-
|
38 |
-
return jsonify({"error": "An error occurred", "details": str(e)}), 500
|
39 |
-
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
+
@app.route('/duckchat', methods=['POST'])
|
8 |
+
def duckchat():
|
|
|
|
|
|
|
9 |
try:
|
10 |
+
# Get the input data from the request
|
11 |
+
input_data = request.json
|
12 |
+
|
13 |
+
if not input_data or 'messages' not in input_data:
|
14 |
+
return jsonify({"error": "Invalid input. 'messages' field is required."}), 400
|
15 |
+
|
16 |
+
# Get the status endpoint to fetch x-vqd-4
|
17 |
+
status_url = "https://duckduckgo.com/duckchat/v1/status"
|
18 |
+
status_headers = {
|
19 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
20 |
+
"x-vqd-accept": "1"
|
21 |
+
}
|
22 |
+
status_response = requests.get(status_url, headers=status_headers)
|
23 |
+
x_vqd_4 = status_response.headers.get("x-vqd-4", "")
|
24 |
+
|
25 |
+
if not x_vqd_4:
|
26 |
+
return jsonify({"error": "Failed to retrieve x-vqd-4 token."}), 500
|
27 |
+
|
28 |
+
# Send the chat message
|
29 |
+
chat_url = "https://duckduckgo.com/duckchat/v1/chat"
|
30 |
+
chat_headers = {
|
31 |
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
|
32 |
+
"x-vqd-4": x_vqd_4
|
33 |
+
}
|
34 |
+
chat_payload = {
|
35 |
+
"messages": input_data['messages'],
|
36 |
+
"model": input_data.get('model', "gpt-4o-mini")
|
37 |
+
}
|
38 |
+
|
39 |
+
chat_response = requests.post(chat_url, headers=chat_headers, json=chat_payload)
|
40 |
+
|
41 |
+
# Check if the response is successful
|
42 |
+
if chat_response.status_code != 200:
|
43 |
+
return jsonify({"error": "Failed to fetch response from DuckDuckGo Chat API."}), chat_response.status_code
|
44 |
+
|
45 |
+
# Process the response
|
46 |
+
response_lines = chat_response.text.splitlines()
|
47 |
+
final_response = ""
|
48 |
+
|
49 |
+
for line in response_lines:
|
50 |
+
try:
|
51 |
+
data = json.loads(line.replace("data: ", ""))
|
52 |
+
if "message" in data:
|
53 |
+
final_response += data["message"]
|
54 |
+
except json.JSONDecodeError:
|
55 |
+
continue
|
56 |
+
|
57 |
+
return jsonify({"response": final_response})
|
58 |
|
59 |
except Exception as e:
|
60 |
+
return jsonify({"error": str(e)}), 500
|
|
|
|
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
app.run(host="0.0.0.0", port=7860)
|