Luciferalive commited on
Commit
2e3e738
·
verified ·
1 Parent(s): 5d9b48f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -17,12 +17,12 @@ import os
17
  import boto3
18
 
19
  # AWS access credentials
20
- access_key = os.getenv("access_key")
21
- secret_key = os.getenv("secret_key")
22
 
23
  # S3 bucket details
24
- bucket_name = os.getenv("bucket_name")
25
- prefix = os.getenv("prefix")
26
 
27
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
28
 
@@ -59,18 +59,22 @@ def compute_cosine_similarity_scores(query, retrieved_docs):
59
  readable_scores = [{"doc": doc, "score": float(score)} for doc, score in zip(retrieved_docs, cosine_scores.flatten())]
60
  return readable_scores
61
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def answer_query_with_similarity(query):
63
  try:
64
  # Fetch files from S3
65
- s3 = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_key)
66
- objects = s3.list_objects_v2(Bucket=bucket_name, Prefix=prefix)
67
-
68
- file_contents = []
69
- for obj in objects.get('Contents', []):
70
- if not obj['Key'].endswith('/'): # Skip directories
71
- response = s3.get_object(Bucket=bucket_name, Key=obj['Key'])
72
- file_content = response['Body'].read()
73
- file_contents.append(file_content)
74
 
75
  all_text = process_files(file_contents)
76
 
 
17
  import boto3
18
 
19
  # AWS access credentials
20
+ access_key = os.getenv("ACCESS_KEY")
21
+ secret_key = os.getenv("SECRET_KEY")
22
 
23
  # S3 bucket details
24
+ bucket_name = os.getenv("BUCKET_NAME")
25
+ prefix = os.getenv("PREFIX")
26
 
27
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
28
 
 
59
  readable_scores = [{"doc": doc, "score": float(score)} for doc, score in zip(retrieved_docs, cosine_scores.flatten())]
60
  return readable_scores
61
 
62
+ def fetch_files_from_s3():
63
+ s3 = boto3.client('s3', aws_access_key_id=access_key, aws_secret_access_key=secret_key)
64
+ objects = s3.list_objects_v2(Bucket=bucket_name, Prefix=prefix)
65
+
66
+ file_contents = []
67
+ for obj in objects.get('Contents', []):
68
+ if not obj['Key'].endswith('/'): # Skip directories
69
+ response = s3.get_object(Bucket=bucket_name, Key=obj['Key'])
70
+ file_content = response['Body'].read()
71
+ file_contents.append(file_content)
72
+ return file_contents
73
+
74
  def answer_query_with_similarity(query):
75
  try:
76
  # Fetch files from S3
77
+ file_contents = fetch_files_from_s3()
 
 
 
 
 
 
 
 
78
 
79
  all_text = process_files(file_contents)
80