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
from time import time as t
|
| 4 |
+
from flask import Flask, jsonify, request
|
| 5 |
+
app = Flask(__name__)
|
| 6 |
+
|
| 7 |
+
@app.route('/search', methods=['POST'])
|
| 8 |
+
def webscout2_search():
|
| 9 |
+
data = request.get_json()
|
| 10 |
+
if 'query' not in data:
|
| 11 |
+
return jsonify({'error': 'Query parameter missing'}), 400
|
| 12 |
+
|
| 13 |
+
query = data['query']
|
| 14 |
+
|
| 15 |
+
with DDGS() as ddgs:
|
| 16 |
+
responses = []
|
| 17 |
+
for i, r in enumerate(ddgs.text(query, region='wt-wt', safesearch='off', timelimit='y')):
|
| 18 |
+
if i == 10: # Limiting the results to 10
|
| 19 |
+
break
|
| 20 |
+
responses.append(r)
|
| 21 |
+
return jsonify(responses)
|
| 22 |
+
|
| 23 |
+
@app.route('/health', methods=['GET'])
|
| 24 |
+
def health_check():
|
| 25 |
+
# You can add more sophisticated checks here if needed
|
| 26 |
+
return jsonify({'status': 'ok'})
|
| 27 |
+
|
| 28 |
+
@app.route('/video', methods=['GET'])
|
| 29 |
+
def webscout_videos():
|
| 30 |
+
params = request.args
|
| 31 |
+
if 'keywords' not in params:
|
| 32 |
+
return jsonify({'error': 'Keywords parameter missing'}), 400
|
| 33 |
+
|
| 34 |
+
keywords = params['keywords']
|
| 35 |
+
|
| 36 |
+
with DDGS() as ddgs:
|
| 37 |
+
responses = []
|
| 38 |
+
for r in ddgs.videos(
|
| 39 |
+
keywords,
|
| 40 |
+
region="wt-wt",
|
| 41 |
+
safesearch="off",
|
| 42 |
+
timelimit="w",
|
| 43 |
+
resolution="high",
|
| 44 |
+
duration="medium",
|
| 45 |
+
max_results=10,
|
| 46 |
+
):
|
| 47 |
+
responses.append(r)
|
| 48 |
+
return jsonify(responses)
|
| 49 |
+
|
| 50 |
+
@app.route('/img', methods=['GET'])
|
| 51 |
+
def webscout2_images():
|
| 52 |
+
params = request.args
|
| 53 |
+
if 'keywords' not in params:
|
| 54 |
+
return jsonify({'error': 'Keywords parameter missing'}), 400
|
| 55 |
+
|
| 56 |
+
keywords = params['keywords']
|
| 57 |
+
|
| 58 |
+
with DDGS() as ddgs:
|
| 59 |
+
responses = []
|
| 60 |
+
for r in ddgs.images(
|
| 61 |
+
keywords,
|
| 62 |
+
region="wt-wt",
|
| 63 |
+
safesearch="off",
|
| 64 |
+
size=None,
|
| 65 |
+
# color="Monochrome",
|
| 66 |
+
type_image=None,
|
| 67 |
+
layout=None,
|
| 68 |
+
license_image=None,
|
| 69 |
+
max_results=10,
|
| 70 |
+
):
|
| 71 |
+
responses.append(r)
|
| 72 |
+
return jsonify(responses)
|
| 73 |
+
|
| 74 |
+
@app.route('/news', methods=['GET'])
|
| 75 |
+
def webscout_news():
|
| 76 |
+
params = request.args
|
| 77 |
+
if 'keywords' not in params:
|
| 78 |
+
return jsonify({'error': 'Keywords parameter missing'}), 400
|
| 79 |
+
|
| 80 |
+
keywords = params['keywords']
|
| 81 |
+
|
| 82 |
+
with DDGS() as ddgs:
|
| 83 |
+
responses = []
|
| 84 |
+
for r in ddgs.news(
|
| 85 |
+
keywords,
|
| 86 |
+
region="wt-wt",
|
| 87 |
+
safesearch="off",
|
| 88 |
+
timelimit="m",
|
| 89 |
+
max_results=10
|
| 90 |
+
):
|
| 91 |
+
responses.append(r)
|
| 92 |
+
return jsonify(responses)
|
| 93 |
+
|
| 94 |
+
@app.route('/int', methods=['GET'])
|
| 95 |
+
def webscout3_search():
|
| 96 |
+
query = request.args.get('query')
|
| 97 |
+
if not query:
|
| 98 |
+
return jsonify({'error': 'Query parameter missing'}), 400
|
| 99 |
+
|
| 100 |
+
# Initialize the DDGS (DuckDuckGo Search) object
|
| 101 |
+
with DDGS() as ddgs:
|
| 102 |
+
responses = []
|
| 103 |
+
for i, r in enumerate(ddgs.text(query, region='wt-wt', safesearch='off', timelimit='y')):
|
| 104 |
+
if i == 2: # Limiting the results to 10
|
| 105 |
+
break
|
| 106 |
+
responses.append(r)
|
| 107 |
+
|
| 108 |
+
# You can process the search results further or customize the response
|
| 109 |
+
# For now, let's return the search results as-is
|
| 110 |
+
return jsonify(responses)
|
| 111 |
+
app = Flask(__name__)
|
| 112 |
+
|
| 113 |
+
@app.route('/translate', methods=['GET'])
|
| 114 |
+
def webscout_translate():
|
| 115 |
+
params = request.args
|
| 116 |
+
if 'keywords' not in params or 'to' not in params:
|
| 117 |
+
return jsonify({'error': 'Keywords or target language parameter missing'}), 400
|
| 118 |
+
|
| 119 |
+
keywords = params['keywords']
|
| 120 |
+
target_language = params['to']
|
| 121 |
+
|
| 122 |
+
with DDGS() as ddgs:
|
| 123 |
+
translation = ddgs.translate(keywords, to=target_language)
|
| 124 |
+
return jsonify(translation)
|
| 125 |
+
|
| 126 |
+
@app.route('/chat', methods=['POST'])
|
| 127 |
+
def chat_gpt():
|
| 128 |
+
user_input = request.json.get('message')
|
| 129 |
+
|
| 130 |
+
messages = [
|
| 131 |
+
{
|
| 132 |
+
"role": "system",
|
| 133 |
+
"content": "Hello! I'm your virtual assistant. How can I help you?"
|
| 134 |
+
}
|
| 135 |
+
]
|
| 136 |
+
|
| 137 |
+
assert user_input is not None
|
| 138 |
+
|
| 139 |
+
messages.append({"role": "user", "content": user_input})
|
| 140 |
+
|
| 141 |
+
response = g4f.ChatCompletion.create(
|
| 142 |
+
model="gpt-4-32k-0613",
|
| 143 |
+
provider=g4f.Provider.GPTalk,
|
| 144 |
+
messages=messages,
|
| 145 |
+
stream=True,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
ms = ""
|
| 149 |
+
for message in response:
|
| 150 |
+
ms += message
|
| 151 |
+
|
| 152 |
+
messages.append({"role": "assistant", "content": ms})
|
| 153 |
+
return jsonify({"response": ms})
|
| 154 |
+
if __name__ == '__main__':
|
| 155 |
+
app.run()
|
| 156 |
+
|