SharryOG commited on
Commit
5bb2bb7
1 Parent(s): 3394abe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -114
app.py CHANGED
@@ -1,119 +1,76 @@
1
- import logging
2
- from typing import Optional
3
-
4
- from flask import Flask, jsonify, request
5
- from webscout import WEBS
6
  import requests
 
 
7
 
8
  app = Flask(__name__)
9
-
10
- TIMEOUT = 10
11
- PROXY = None
12
-
13
- @app.route('/api/search', methods=['GET'])
14
- def search_text():
15
- query = request.args.get('q', '')
16
- max_results = request.args.get('max_results', 10, type=int)
17
- timelimit = request.args.get('timelimit', None)
18
- safesearch = request.args.get('safesearch', 'moderate')
19
- region = request.args.get('region', 'wt-wt')
20
- WEBS_instance = WEBS() # Instantiate WEBS without context manager
21
- results = []
22
- with WEBS() as webs:
23
- for result in enumerate(WEBS_instance.text(query, max_results=max_results, timelimit=timelimit, safesearch=safesearch, region=region)):
24
- results.append(result)
25
-
26
- return jsonify({'results': results})
27
-
28
-
29
- @app.route('/api/images', methods=['GET'])
30
- def search_images():
31
- query = request.args.get('q', '')
32
- max_results = request.args.get('max_results', 10, type=int)
33
- safesearch = request.args.get('safesearch', 'moderate')
34
- region = request.args.get('region', 'wt-wt')
35
- WEBS_instance = WEBS()
36
- results = []
37
- with WEBS() as webs:
38
- for result in enumerate(WEBS_instance.images(query, max_results=max_results, safesearch=safesearch, region=region)):
39
- results.append(result)
40
-
41
- return jsonify({'results': results})
42
-
43
- @app.route('/api/videos', methods=['GET'])
44
- def search_videos():
45
- query = request.args.get('q', '')
46
- max_results = request.args.get('max_results', 10, type=int)
47
- safesearch = request.args.get('safesearch', 'moderate')
48
- region = request.args.get('region', 'wt-wt')
49
- timelimit = request.args.get('timelimit', None)
50
- resolution = request.args.get('resolution', None)
51
- duration = request.args.get('duration', None)
52
- WEBS_instance = WEBS()
53
- results = []
54
- with WEBS() as webs:
55
- for result in enumerate(WEBS_instance.videos(query, max_results=max_results, safesearch=safesearch, region=region, timelimit=timelimit, resolution=resolution, duration=duration)):
56
- results.append(result)
57
-
58
- return jsonify({'results': results})
59
-
60
- @app.route('/api/news', methods=['GET'])
61
- def search_news():
62
- query = request.args.get('q', '')
63
- max_results = request.args.get('max_results', 10, type=int)
64
- safesearch = request.args.get('safesearch', 'moderate')
65
- region = request.args.get('region', 'wt-wt')
66
- timelimit = request.args.get('timelimit', None)
67
- WEBS_instance = WEBS()
68
- results = []
69
- with WEBS() as webs:
70
- for result in enumerate(WEBS_instance.news(query, max_results=max_results, safesearch=safesearch, region=region, timelimit=timelimit)):
71
- results.append(result)
72
-
73
- return jsonify({'results': results})
74
-
75
- @app.route('/api/maps', methods=['GET'])
76
- def search_maps():
77
- query = request.args.get('q', '')
78
- place = request.args.get('place', None)
79
- max_results = request.args.get('max_results', 10, type=int)
80
- WEBS_instance = WEBS()
81
- results = []
82
- with WEBS() as webs:
83
- for result in enumerate(WEBS_instance.maps(query, place=place, max_results=max_results)):
84
- results.append(result)
85
-
86
- return jsonify({'results': results})
87
-
88
- @app.route('/api/translate', methods=['GET'])
89
- def translate_text():
90
- query = request.args.get('q', '')
91
- to_lang = request.args.get('to', 'en')
92
- WEBS_instance = WEBS()
93
- with WEBS() as webs:
94
- translation = enumerate(WEBS_instance.translate(query, to=to_lang))
95
-
96
- return jsonify({'translation': translation})
97
-
98
- @app.route('/api/suggestions', methods=['GET'])
99
- def search_suggestions():
100
- query = request.args.get('q', '')
101
- if not query:
102
- return jsonify({'error': 'Query parameter missing'})
103
-
104
- results = []
105
- try:
106
- with WEBS() as webs:
107
- for result in webs.suggestions(query):
108
- results.append(result)
109
- except Exception as e:
110
- return jsonify({'error': str(e)}), 500
111
-
112
- return jsonify({'results': results})
113
-
114
- @app.route('/api/health', methods=['GET'])
115
- def health_check():
116
- return jsonify({'status': 'working'})
117
 
118
  if __name__ == '__main__':
119
- app.run()
 
1
+ from flask import Flask, request, jsonify
 
 
 
 
2
  import requests
3
+ from typing import List, Dict, Union
4
+ import json
5
 
6
  app = Flask(__name__)
7
+ models = ['cognitivecomputations/dolphin-2.6-mixtral-8x7b', 'databricks/dbrx-instruct', 'google/gemma-1.1-7b-it', 'HuggingFaceH4/zephyr-orpo-141b-A35b-v0.1', 'lizpreciatior/lzlv_70b_fp16_hf', 'meta-llama/Meta-Llama-3-70B-Instruct', 'meta-llama/Meta-Llama-3-8B-Instruct', 'microsoft/WizardLM-2-7B', 'microsoft/WizardLM-2-8x22B', 'mistralai/Mixtral-8x7B-Instruct-v0.1', 'mistralai/Mixtral-8x22B-Instruct-v0.1', 'mistralai/Mistral-7B-Instruct-v0.2', 'openchat/openchat-3.6-8b']
8
+ class LLM:
9
+ def __init__(self, model: str):
10
+ self.model = model
11
+ self.conversation_history = [{"role": "system", "content": "You are a Helpful AI."}]
12
+
13
+ def chat(self, messages: List[Dict[str, str]], system_message: str = None) -> Union[str, None]:
14
+ if system_message is not None:
15
+ self.conversation_history.insert(0, {"role": "system", "content": system_message})
16
+ all_messages = self.conversation_history + messages
17
+
18
+ url = "https://api.deepinfra.com/v1/openai/chat/completions"
19
+ headers = {
20
+ 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
21
+ 'Accept-Language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
22
+ 'Cache-Control': 'no-cache',
23
+ 'Connection': 'keep-alive',
24
+ 'Content-Type': 'application/json',
25
+ 'Origin': 'https://deepinfra.com',
26
+ 'Pragma': 'no-cache',
27
+ 'Referer': 'https://deepinfra.com/',
28
+ 'Sec-Fetch-Dest': 'empty',
29
+ 'Sec-Fetch-Mode': 'cors',
30
+ 'Sec-Fetch-Site': 'same-site',
31
+ 'X-Deepinfra-Source': 'web-embed',
32
+ 'accept': 'text/event-stream',
33
+ 'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
34
+ 'sec-ch-ua-mobile': '?0',
35
+ 'sec-ch-ua-platform': '"macOS"'
36
+ }
37
+ data = json.dumps(
38
+ {
39
+ 'model': self.model,
40
+ 'messages': all_messages,
41
+ 'temperature': 0.7,
42
+ 'max_tokens': 8028,
43
+ 'stop': [],
44
+ 'stream': False
45
+ }, separators=(',', ':')
46
+ )
47
+ try:
48
+ result = requests.post(url=url, data=data, headers=headers)
49
+ return result.json()['choices'][0]['message']['content']
50
+ except:
51
+ return None
52
+
53
+ def GenerativeIO(text, Model, System_Prompt):
54
+ llm = LLM(model=Model)
55
+ messages = [
56
+ {"role": "system", "content": text},
57
+ {"role": "user", "content": System_Prompt}
58
+ ]
59
+ response = llm.chat(messages)
60
+ return response
61
+
62
+ @app.route('/generate', methods=['POST'])
63
+ def generate():
64
+ data = request.get_json()
65
+ text = data.get('text')
66
+ Model = data.get('Model')
67
+ System_Prompt = data.get('System_Prompt')
68
+ response = GenerativeIO(text, Model, System_Prompt)
69
+ return jsonify({'response': response})
70
+
71
+ @app.route('/models', methods=['GET'])
72
+ def get_models():
73
+ return jsonify(models)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
 
75
  if __name__ == '__main__':
76
+ app.run(debug=True)