Dooratre commited on
Commit
5fa78e9
·
verified ·
1 Parent(s): cc4fd9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -32
app.py CHANGED
@@ -1,42 +1,63 @@
1
- import requests
2
  from flask import Flask, request, jsonify
 
 
3
 
4
  app = Flask(__name__)
5
 
6
- # Your PythonAnywhere API endpoint
7
- PYTHONANYWHERE_URL = "https://omarnuwara.pythonanywhere.com/get-response"
8
-
9
- @app.route('/chat', methods=['POST'])
10
- def chat():
11
  try:
12
- # Ensure the request contains JSON and a "message" key
13
- user_input = request.json.get("message", "") if request.is_json else None
14
-
15
- if not user_input:
16
- return jsonify({"error": "No message provided in the request."}), 400
17
-
18
- # Create the payload to send to PythonAnywhere
19
- data = {"message": user_input}
20
-
21
- # Forward the request to PythonAnywhere
22
- response = requests.post(PYTHONANYWHERE_URL, json=data)
23
-
24
- if response.status_code == 200:
25
- response_json = response.json()
26
- ai_response = response_json.get("response", "No 'response' key found in the JSON.")
27
- ai_response = ai_response.encode('latin1').decode('utf-8', 'ignore')
28
- return (ai_response)
29
- else:
30
- # Handle errors from PythonAnywhere
31
- return jsonify({
32
- "error": "Error from PythonAnywhere",
33
- "details": response.text
34
- }), response.status_code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
  except Exception as e:
37
- # Catch any unexpected errors
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)