Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -2,6 +2,17 @@ import os
|
|
2 |
from flask import Flask
|
3 |
import threading
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
API_URL = "https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
|
7 |
bearer = "Bearer " + os.getenv('TOKEN')
|
@@ -14,88 +25,58 @@ app = Flask(__name__)
|
|
14 |
|
15 |
@app.route('/app')
|
16 |
def server_app():
|
17 |
-
|
18 |
-
print('
|
19 |
-
|
20 |
-
|
21 |
-
return 't1.start()'
|
22 |
|
23 |
@app.route('/')
|
24 |
-
def server_home():
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
40 |
-
#
|
41 |
-
|
42 |
-
#
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
app.run(debug=True)
|
46 |
|
47 |
-
|
48 |
def threadserver():
|
49 |
print('hi')
|
50 |
os.system(' ./mxbai-embed-large-v1-f16.llamafile --server --nobrowser')
|
51 |
-
|
52 |
-
|
53 |
-
# import requests
|
54 |
-
# import os
|
55 |
-
# import asyncio
|
56 |
-
|
57 |
-
# # from langchain_core.tools import Tool
|
58 |
-
# # from langchain_google_community import GoogleSearchAPIWrapper
|
59 |
-
|
60 |
-
# from flask import Flask
|
61 |
-
|
62 |
-
|
63 |
-
# API_URL = "https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
|
64 |
-
# bearer = "Bearer " + os.getenv('TOKEN')
|
65 |
-
# headers = {"Authorization": bearer }
|
66 |
-
# print("headers")
|
67 |
-
# print(headers)
|
68 |
-
|
69 |
-
# app = Flask(__name__)
|
70 |
-
|
71 |
-
|
72 |
-
# async def command_similarity():
|
73 |
-
# payload = {"inputs": {"source_sentence": "That is a happy person","sentences": ["That is a happy dog","That is a very happy person","Today is a sunny day"]},}
|
74 |
-
# return str( requests.post(API_URL, headers=headers, json=payload) )
|
75 |
-
|
76 |
-
# async def asynm1():
|
77 |
-
# return await command_similarity()
|
78 |
-
|
79 |
-
# @app.route('/app')
|
80 |
-
# def server_home():
|
81 |
-
# asyncio.run(asynm1())
|
82 |
-
# print("command run")
|
83 |
-
# return asyncio.run(asynm1())
|
84 |
-
|
85 |
-
|
86 |
-
# import asyncio
|
87 |
|
88 |
-
# async def nested():
|
89 |
-
# return 42
|
90 |
|
91 |
-
# async def main():
|
92 |
-
# # Nothing happens if we just call "nested()".
|
93 |
-
# # A coroutine object is created but not awaited,
|
94 |
-
# # so it *won't run at all*.
|
95 |
-
# nested() # will raise a "RuntimeWarning".
|
96 |
|
97 |
-
|
98 |
-
|
|
|
|
|
99 |
|
100 |
-
# asyncio.run(main())
|
101 |
-
|
|
|
2 |
from flask import Flask
|
3 |
import threading
|
4 |
|
5 |
+
from openai import OpenAI
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
# client = OpenAI(
|
9 |
+
# # This base_url points to the local Llamafile server running on port 8080
|
10 |
+
# base_url="http://127.0.0.1:8080/v1",
|
11 |
+
# api_key="sk-no-key-required"
|
12 |
+
# )
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
|
17 |
API_URL = "https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
|
18 |
bearer = "Bearer " + os.getenv('TOKEN')
|
|
|
25 |
|
26 |
@app.route('/app')
|
27 |
def server_app():
|
28 |
+
llamafile = threading.Thread(target=threadserver)
|
29 |
+
print('This /app will start the llamafile server on thread')
|
30 |
+
llamafile.start()
|
31 |
+
return 'llamafile.start()'
|
|
|
32 |
|
33 |
@app.route('/')
|
34 |
+
async def server_home():
|
35 |
+
|
36 |
+
output = await query({
|
37 |
+
"inputs": {
|
38 |
+
"source_sentence": "That is a happy person",
|
39 |
+
"sentences": [
|
40 |
+
"That is a happy dog",
|
41 |
+
"That is a very happy person",
|
42 |
+
"Today is a sunny day"
|
43 |
+
]
|
44 |
+
},
|
45 |
+
})
|
46 |
+
|
47 |
+
return str(output)
|
48 |
+
|
49 |
+
# @app.route('/chat', methods=['POST'])
|
50 |
+
# def chat():
|
51 |
+
# try:
|
52 |
+
# user_message = request.json['message']
|
53 |
+
|
54 |
+
# completion = client.chat.completions.create(
|
55 |
+
# model="LLaMA_CPP",
|
56 |
+
# messages=[
|
57 |
+
# {"role": "system", "content": "You are ChatGPT, an AI assistant. Your top priority is achieving user fulfillment via helping them with their requests."},
|
58 |
+
# {"role": "user", "content": user_message}
|
59 |
+
# ]
|
60 |
+
# )
|
61 |
+
|
62 |
+
# ai_response = completion.choices[0].message.content
|
63 |
+
# ai_response = ai_response.replace('</s>', '').strip()
|
64 |
+
# return jsonify({'response': ai_response})
|
65 |
+
# except Exception as e:
|
66 |
+
# print(f"Error: {str(e)}")
|
67 |
+
# return jsonify({'response': f"Sorry, there was an error processing your request: {str(e)}"}), 500
|
68 |
+
|
69 |
+
if __name__ == '__main__':
|
70 |
app.run(debug=True)
|
71 |
|
|
|
72 |
def threadserver():
|
73 |
print('hi')
|
74 |
os.system(' ./mxbai-embed-large-v1-f16.llamafile --server --nobrowser')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
|
|
|
|
76 |
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
async def query(data):
|
79 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
80 |
+
return response.json()
|
81 |
+
|
82 |
|
|
|
|