Update app.py
Browse files
app.py
CHANGED
@@ -1,119 +1,76 @@
|
|
1 |
-
import
|
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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
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)
|