Abhaykoul commited on
Commit
be73bfc
1 Parent(s): 39a2966

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -22
app.py CHANGED
@@ -1,50 +1,52 @@
1
  from flask import Flask, jsonify, request
 
2
  from webscout import WEBS
3
  from webscout.LLM import LLM
4
 
5
  app = Flask(__name__)
6
 
 
 
 
 
 
7
  @app.route('/search', methods=['POST'])
8
  def WEBScout2_search_search():
9
  data = request.get_json()
10
  if 'query' not in data:
11
  return jsonify({'error': 'Query parameter missing'})
12
-
13
  query = data['query']
14
- limit = data.get('limit', 10) # Default limit to 10 if not provided
15
- WEBS_instance = WEBS() # Instantiate WEBS without context manager
16
  responses = []
17
  for i, r in enumerate(WEBS_instance.text(query, region='wt-wt', safesearch='off', timelimit='y')):
18
  responses.append(r)
19
  return jsonify(responses)
20
-
21
  @app.route('/int', methods=['GET'])
22
  def WEBScout3_search_search_search_search():
23
  query = request.args.get('q')
24
  if not query:
25
  return jsonify({'error': 'Query parameter missing'})
26
-
27
- limit = request.args.get('limit', default=10, type=int) # Default limit to 10 if not provided
28
- WEBS_instance = WEBS() # Instantiate WEBS without context manager
29
  responses = []
30
  for i, r in enumerate(WEBS_instance.text(query, region='wt-wt', safesearch='off', timelimit='y')):
31
- if i == limit: # Use the custom limit
32
  break
33
  responses.append(r)
34
  return jsonify(responses)
35
 
36
-
37
  @app.route('/mws', methods=['POST'])
38
  def WEBScout_search():
39
  data = request.get_json()
40
  if 'query' not in data:
41
  return jsonify({'error': 'Query parameter missing'})
42
-
43
  query = data['query']
44
- WEBS_instance = WEBS() # Instantiate WEBS without context manager
45
  responses = []
46
  for i, r in enumerate(WEBS_instance.text(query, region='wt-wt', safesearch='off', timelimit='y')):
47
- if i == 2: # Limiting the results to 2
48
  break
49
  responses.append(r)
50
  return jsonify(responses)
@@ -58,9 +60,8 @@ def WEBScout_videos():
58
  params = request.args
59
  if 'keywords' not in params:
60
  return jsonify({'error': 'Keywords parameter missing'})
61
-
62
  keywords = params['keywords']
63
- WEBS_instance = WEBS() # Instantiate WEBS without context manager
64
  responses = []
65
  for r in WEBS_instance.videos(
66
  keywords,
@@ -79,9 +80,8 @@ def WEBScout2_images():
79
  params = request.args
80
  if 'keywords' not in params:
81
  return jsonify({'error': 'Keywords parameter missing'})
82
-
83
  keywords = params['keywords']
84
- WEBS_instance = WEBS() # Instantiate WEBS without context manager
85
  responses = []
86
  for r in WEBS_instance.images(
87
  keywords,
@@ -102,7 +102,7 @@ def WEBScout_news():
102
  if 'keywords' not in params:
103
  return jsonify({'error': 'Keywords parameter missing'})
104
  keywords = params['keywords']
105
- WEBS_instance = WEBS() # Instantiate WEBS without context manager
106
  responses = []
107
  for r in WEBS_instance.news(
108
  keywords,
@@ -114,7 +114,6 @@ def WEBScout_news():
114
  responses.append(r)
115
  return jsonify(responses)
116
 
117
-
118
  @app.route('/translate', methods=['GET'])
119
  def WEBScout_translate():
120
  params = request.args
@@ -122,7 +121,7 @@ def WEBScout_translate():
122
  return jsonify({'error': 'Keywords or target language parameter missing'})
123
  keywords = params['keywords']
124
  target_language = params['to']
125
- WEBS_instance = WEBS() # Instantiate WEBS without context manager
126
  translation = WEBS_instance.translate(keywords, to=target_language)
127
  return jsonify(translation)
128
 
@@ -131,9 +130,8 @@ def chat_gpt():
131
  user_input = request.json.get('message')
132
  if user_input is None:
133
  return jsonify({'error': 'Message parameter missing'})
134
-
135
- llm = LLM("mistralai/Mixtral-8x7B-Instruct-v0.1") # Initialize the LLM class with a model
136
- response = llm.mistral_chat([{"role": "user", "content": user_input}]) # Call the mistral_chat method
137
  return jsonify({"response": response})
138
 
139
  if __name__ == '__main__':
 
1
  from flask import Flask, jsonify, request
2
+ import os
3
  from webscout import WEBS
4
  from webscout.LLM import LLM
5
 
6
  app = Flask(__name__)
7
 
8
+ # Create a directory for the .cache if it doesn't exist
9
+ cache_dir = os.path.join(os.path.expanduser("~"), ".cache")
10
+ if not os.path.exists(cache_dir):
11
+ os.makedirs(cache_dir)
12
+
13
  @app.route('/search', methods=['POST'])
14
  def WEBScout2_search_search():
15
  data = request.get_json()
16
  if 'query' not in data:
17
  return jsonify({'error': 'Query parameter missing'})
 
18
  query = data['query']
19
+ limit = data.get('limit', 10) # Default limit to 10 if not provided
20
+ WEBS_instance = WEBS() # Instantiate WEBS without context manager
21
  responses = []
22
  for i, r in enumerate(WEBS_instance.text(query, region='wt-wt', safesearch='off', timelimit='y')):
23
  responses.append(r)
24
  return jsonify(responses)
25
+
26
  @app.route('/int', methods=['GET'])
27
  def WEBScout3_search_search_search_search():
28
  query = request.args.get('q')
29
  if not query:
30
  return jsonify({'error': 'Query parameter missing'})
31
+ limit = request.args.get('limit', default=10, type=int) # Default limit to 10 if not provided
32
+ WEBS_instance = WEBS() # Instantiate WEBS without context manager
 
33
  responses = []
34
  for i, r in enumerate(WEBS_instance.text(query, region='wt-wt', safesearch='off', timelimit='y')):
35
+ if i == limit: # Use the custom limit
36
  break
37
  responses.append(r)
38
  return jsonify(responses)
39
 
 
40
  @app.route('/mws', methods=['POST'])
41
  def WEBScout_search():
42
  data = request.get_json()
43
  if 'query' not in data:
44
  return jsonify({'error': 'Query parameter missing'})
 
45
  query = data['query']
46
+ WEBS_instance = WEBS() # Instantiate WEBS without context manager
47
  responses = []
48
  for i, r in enumerate(WEBS_instance.text(query, region='wt-wt', safesearch='off', timelimit='y')):
49
+ if i == 2: # Limiting the results to 2
50
  break
51
  responses.append(r)
52
  return jsonify(responses)
 
60
  params = request.args
61
  if 'keywords' not in params:
62
  return jsonify({'error': 'Keywords parameter missing'})
 
63
  keywords = params['keywords']
64
+ WEBS_instance = WEBS() # Instantiate WEBS without context manager
65
  responses = []
66
  for r in WEBS_instance.videos(
67
  keywords,
 
80
  params = request.args
81
  if 'keywords' not in params:
82
  return jsonify({'error': 'Keywords parameter missing'})
 
83
  keywords = params['keywords']
84
+ WEBS_instance = WEBS() # Instantiate WEBS without context manager
85
  responses = []
86
  for r in WEBS_instance.images(
87
  keywords,
 
102
  if 'keywords' not in params:
103
  return jsonify({'error': 'Keywords parameter missing'})
104
  keywords = params['keywords']
105
+ WEBS_instance = WEBS() # Instantiate WEBS without context manager
106
  responses = []
107
  for r in WEBS_instance.news(
108
  keywords,
 
114
  responses.append(r)
115
  return jsonify(responses)
116
 
 
117
  @app.route('/translate', methods=['GET'])
118
  def WEBScout_translate():
119
  params = request.args
 
121
  return jsonify({'error': 'Keywords or target language parameter missing'})
122
  keywords = params['keywords']
123
  target_language = params['to']
124
+ WEBS_instance = WEBS() # Instantiate WEBS without context manager
125
  translation = WEBS_instance.translate(keywords, to=target_language)
126
  return jsonify(translation)
127
 
 
130
  user_input = request.json.get('message')
131
  if user_input is None:
132
  return jsonify({'error': 'Message parameter missing'})
133
+ llm = LLM("mistralai/Mixtral-8x7B-Instruct-v0.1") # Initialize the LLM class with a model
134
+ response = llm.mistral_chat([{"role": "user", "content": user_input}]) # Call the mistral_chat method
 
135
  return jsonify({"response": response})
136
 
137
  if __name__ == '__main__':