Tonic commited on
Commit
b95c00f
·
1 Parent(s): 3762672

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -7
app.py CHANGED
@@ -10,6 +10,7 @@ from langchain.chains import RetrievalQA
10
  import os
11
  import urllib.request
12
  import ssl
 
13
  import tempfile
14
  import mimetypes
15
  from dotenv import load_dotenv
@@ -127,7 +128,7 @@ def embed_pdf(file, filename, collection_name, file_type):
127
  context = ssl._create_unverified_context()
128
  with urllib.request.urlopen(file, context=context) as response, open(filename, 'wb') as out_file:
129
  data = response.read()
130
- out_file.write(data_bytes)
131
  file_path = filename
132
  except Exception as e:
133
  return {"error": f"Error downloading file from URL: {e}"}
@@ -139,7 +140,7 @@ def embed_pdf(file, filename, collection_name, file_type):
139
  file_content = file
140
  file_path = os.path.join('./', filename)
141
  with open(file_path, 'wb') as f:
142
- f.write(file_bytes)
143
  else:
144
  return {"error": "Invalid file type"}
145
 
@@ -150,18 +151,14 @@ def embed_pdf(file, filename, collection_name, file_type):
150
  docs = loader.load()
151
 
152
  # Generate embeddings and store documents in Weaviate
153
- embeddings = CohereEmbeddings(model="embed-multilingual-v2.0", cohere_api_key=cohere_api_key)
154
  for doc in docs:
155
- embedding = embeddings.embed([doc['text']])
156
  weaviate_document = {
157
  "text": doc['text'],
158
  "embedding": embedding
159
  }
160
  client.data_object.create(data_object=weaviate_document, class_name=collection_name)
161
 
162
- # Clean up if a temporary file was created
163
- if isinstance(file, bytes):
164
- os.remove(file_path)
165
  return {"message": f"Documents embedded in Weaviate collection '{collection_name}'"}
166
 
167
  def retrieve_info(query):
 
10
  import os
11
  import urllib.request
12
  import ssl
13
+ import bitsandbytes
14
  import tempfile
15
  import mimetypes
16
  from dotenv import load_dotenv
 
128
  context = ssl._create_unverified_context()
129
  with urllib.request.urlopen(file, context=context) as response, open(filename, 'wb') as out_file:
130
  data = response.read()
131
+ out_file.write(data)
132
  file_path = filename
133
  except Exception as e:
134
  return {"error": f"Error downloading file from URL: {e}"}
 
140
  file_content = file
141
  file_path = os.path.join('./', filename)
142
  with open(file_path, 'wb') as f:
143
+ f.write(file)
144
  else:
145
  return {"error": "Invalid file type"}
146
 
 
151
  docs = loader.load()
152
 
153
  # Generate embeddings and store documents in Weaviate
 
154
  for doc in docs:
155
+ embedding = vectorstore.embedding.embed([doc['text']])
156
  weaviate_document = {
157
  "text": doc['text'],
158
  "embedding": embedding
159
  }
160
  client.data_object.create(data_object=weaviate_document, class_name=collection_name)
161
 
 
 
 
162
  return {"message": f"Documents embedded in Weaviate collection '{collection_name}'"}
163
 
164
  def retrieve_info(query):