Spaces:
Build error
Build error
Merge branch 'main' of https://huggingface.co/spaces/Tonic/01aiYi-NvidiaEmbed
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
# app.py
|
2 |
import spaces
|
3 |
from torch.nn import DataParallel
|
4 |
from torch import Tensor
|
@@ -10,19 +10,16 @@ from langchain_chroma import Chroma
|
|
10 |
from chromadb import Documents, EmbeddingFunction, Embeddings
|
11 |
from chromadb.config import Settings
|
12 |
import chromadb #import HttpClient
|
13 |
-
import os
|
|
|
14 |
import re
|
15 |
import uuid
|
16 |
import gradio as gr
|
17 |
import torch
|
18 |
import torch.nn.functional as F
|
19 |
from dotenv import load_dotenv
|
20 |
-
from utils import load_env_variables, parse_and_route
|
21 |
-
from globalvars import API_BASE, intention_prompt, tasks, system_message, model_name
|
22 |
-
# import time
|
23 |
-
# import httpx
|
24 |
-
|
25 |
-
|
26 |
|
27 |
load_dotenv()
|
28 |
|
@@ -31,6 +28,13 @@ os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
|
|
31 |
os.environ['CUDA_CACHE_DISABLE'] = '1'
|
32 |
|
33 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
### Utils
|
36 |
hf_token, yi_token = load_env_variables()
|
@@ -110,7 +114,7 @@ class EmbeddingGenerator:
|
|
110 |
matches = pattern.findall(metadata_output)
|
111 |
metadata = {key: value for key, value in matches}
|
112 |
return metadata
|
113 |
-
|
114 |
class MyEmbeddingFunction(EmbeddingFunction):
|
115 |
def __init__(self, model_name: str, token: str, intention_client):
|
116 |
self.model_name = model_name
|
@@ -140,7 +144,7 @@ def initialize_chroma(collection_name: str, embedding_function: MyEmbeddingFunct
|
|
140 |
|
141 |
def add_documents_to_chroma(documents: list, embedding_function: MyEmbeddingFunction):
|
142 |
for doc in documents:
|
143 |
-
embeddings, metadata = embedding_function.
|
144 |
for embedding, meta in zip(embeddings, metadata):
|
145 |
chroma_collection.add(
|
146 |
ids=[str(uuid.uuid1())],
|
@@ -150,7 +154,7 @@ def add_documents_to_chroma(documents: list, embedding_function: MyEmbeddingFunc
|
|
150 |
)
|
151 |
|
152 |
def query_chroma(query_text: str, embedding_function: MyEmbeddingFunction):
|
153 |
-
query_embeddings, query_metadata = embedding_function.
|
154 |
result_docs = chroma_collection.query(
|
155 |
query_texts=[query_text],
|
156 |
n_results=2
|
@@ -160,7 +164,7 @@ def query_chroma(query_text: str, embedding_function: MyEmbeddingFunction):
|
|
160 |
# Initialize clients
|
161 |
intention_client = OpenAI(api_key=yi_token, base_url=API_BASE)
|
162 |
embedding_generator = EmbeddingGenerator(model_name=model_name, token=hf_token, intention_client=intention_client)
|
163 |
-
embedding_function = MyEmbeddingFunction(
|
164 |
chroma_db = initialize_chroma(collection_name="Tonic-instruct", embedding_function=embedding_function)
|
165 |
|
166 |
def respond(
|
@@ -199,7 +203,7 @@ def upload_documents(files):
|
|
199 |
return "Documents uploaded and processed successfully!"
|
200 |
|
201 |
def query_documents(query):
|
202 |
-
results = query_chroma(query)
|
203 |
return "\n\n".join([result.content for result in results])
|
204 |
|
205 |
with gr.Blocks() as demo:
|
@@ -226,4 +230,4 @@ with gr.Blocks() as demo:
|
|
226 |
|
227 |
if __name__ == "__main__":
|
228 |
# os.system("chroma run --host localhost --port 8000 &")
|
229 |
-
demo.launch()
|
|
|
1 |
+
# app.py
|
2 |
import spaces
|
3 |
from torch.nn import DataParallel
|
4 |
from torch import Tensor
|
|
|
10 |
from chromadb import Documents, EmbeddingFunction, Embeddings
|
11 |
from chromadb.config import Settings
|
12 |
import chromadb #import HttpClient
|
13 |
+
import os
|
14 |
+
import tempfile
|
15 |
import re
|
16 |
import uuid
|
17 |
import gradio as gr
|
18 |
import torch
|
19 |
import torch.nn.functional as F
|
20 |
from dotenv import load_dotenv
|
21 |
+
from utils import load_env_variables, parse_and_route, escape_special_characters
|
22 |
+
from globalvars import API_BASE, intention_prompt, tasks, system_message, model_name, metadata_prompt
|
|
|
|
|
|
|
|
|
23 |
|
24 |
load_dotenv()
|
25 |
|
|
|
28 |
os.environ['CUDA_CACHE_DISABLE'] = '1'
|
29 |
|
30 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
31 |
+
|
32 |
+
# Ensure the temporary directory exists
|
33 |
+
temp_dir = '/tmp/gradio/'
|
34 |
+
os.makedirs(temp_dir, exist_ok=True)
|
35 |
+
|
36 |
+
# Set Gradio cache directory
|
37 |
+
gr.components.file.GRADIO_CACHE = temp_dir
|
38 |
|
39 |
### Utils
|
40 |
hf_token, yi_token = load_env_variables()
|
|
|
114 |
matches = pattern.findall(metadata_output)
|
115 |
metadata = {key: value for key, value in matches}
|
116 |
return metadata
|
117 |
+
|
118 |
class MyEmbeddingFunction(EmbeddingFunction):
|
119 |
def __init__(self, model_name: str, token: str, intention_client):
|
120 |
self.model_name = model_name
|
|
|
144 |
|
145 |
def add_documents_to_chroma(documents: list, embedding_function: MyEmbeddingFunction):
|
146 |
for doc in documents:
|
147 |
+
embeddings, metadata = embedding_function.create_embedding_generator().compute_embeddings(doc)
|
148 |
for embedding, meta in zip(embeddings, metadata):
|
149 |
chroma_collection.add(
|
150 |
ids=[str(uuid.uuid1())],
|
|
|
154 |
)
|
155 |
|
156 |
def query_chroma(query_text: str, embedding_function: MyEmbeddingFunction):
|
157 |
+
query_embeddings, query_metadata = embedding_function.create_embedding_generator().compute_embeddings(query_text)
|
158 |
result_docs = chroma_collection.query(
|
159 |
query_texts=[query_text],
|
160 |
n_results=2
|
|
|
164 |
# Initialize clients
|
165 |
intention_client = OpenAI(api_key=yi_token, base_url=API_BASE)
|
166 |
embedding_generator = EmbeddingGenerator(model_name=model_name, token=hf_token, intention_client=intention_client)
|
167 |
+
embedding_function = MyEmbeddingFunction(model_name=model_name, token=hf_token, intention_client=intention_client)
|
168 |
chroma_db = initialize_chroma(collection_name="Tonic-instruct", embedding_function=embedding_function)
|
169 |
|
170 |
def respond(
|
|
|
203 |
return "Documents uploaded and processed successfully!"
|
204 |
|
205 |
def query_documents(query):
|
206 |
+
results = query_chroma(query, embedding_function)
|
207 |
return "\n\n".join([result.content for result in results])
|
208 |
|
209 |
with gr.Blocks() as demo:
|
|
|
230 |
|
231 |
if __name__ == "__main__":
|
232 |
# os.system("chroma run --host localhost --port 8000 &")
|
233 |
+
demo.launch()
|