Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ title_mapping = {
|
|
| 14 |
'shipping.txt': 'shipping_data'
|
| 15 |
}
|
| 16 |
|
| 17 |
-
#
|
| 18 |
for file_name in os.listdir('.'):
|
| 19 |
if file_name in title_mapping:
|
| 20 |
try:
|
|
@@ -26,22 +26,29 @@ for file_name in os.listdir('.'):
|
|
| 26 |
except Exception as e:
|
| 27 |
print(f"Error processing file {file_name}: {e}")
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
try:
|
| 36 |
-
genai.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Function to embed text using the Google Generative AI AP
|
| 41 |
-
def embed_text(text):
|
| 42 |
-
return genai.embed_content(model='models/embedding-001', content=text, task_type='retrieval_document')['embedding']
|
| 43 |
|
| 44 |
-
#
|
| 45 |
if 'Embeddings' not in df.columns:
|
| 46 |
df['Embeddings'] = df['Text'].apply(embed_text)
|
| 47 |
|
|
@@ -52,29 +59,31 @@ def query_similarity_score(query, vector):
|
|
| 52 |
|
| 53 |
# Function to get the most similar document based on the query
|
| 54 |
def most_similar_document(query):
|
| 55 |
-
# Create a local DataFrame copy to store similarity scores
|
| 56 |
local_df = df.copy()
|
| 57 |
-
# Calculate similarity for all rows
|
| 58 |
local_df['Similarity'] = local_df['Embeddings'].apply(lambda vector: query_similarity_score(query, vector))
|
| 59 |
-
# Sort by similarity score and retrieve the top match
|
| 60 |
most_similar = local_df.sort_values('Similarity', ascending=False).iloc[0]
|
| 61 |
return most_similar['Title'], most_similar['Text']
|
| 62 |
|
| 63 |
# Function to generate a response using the RAG approach
|
| 64 |
def RAG(query):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
| 71 |
iface = gr.Interface(
|
| 72 |
fn=RAG, # Main function to handle the query
|
| 73 |
inputs=[
|
| 74 |
gr.Textbox(label="Enter Your Query"), # Input for the user's query
|
| 75 |
],
|
| 76 |
outputs=gr.Textbox(label="Response"), # Output for the generated response
|
| 77 |
-
title="Patrick's Multilingual Query
|
| 78 |
)
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
| 14 |
'shipping.txt': 'shipping_data'
|
| 15 |
}
|
| 16 |
|
| 17 |
+
# Process relevant files in the current directory
|
| 18 |
for file_name in os.listdir('.'):
|
| 19 |
if file_name in title_mapping:
|
| 20 |
try:
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
print(f"Error processing file {file_name}: {e}")
|
| 28 |
|
| 29 |
+
# Get the Google API key from environment variables
|
| 30 |
+
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 31 |
+
if not GEMINI_API_KEY:
|
| 32 |
+
raise EnvironmentError("Error: Gemini API key not found. Please set the GOOGLE_API_KEY environment variable.")
|
| 33 |
|
| 34 |
+
# Configure the Gemini API
|
| 35 |
+
try:
|
| 36 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
raise RuntimeError(f"Error: Failed to configure the Gemini API. Details: {e}")
|
| 39 |
|
| 40 |
+
# Function to embed text using the Google Generative AI API
|
| 41 |
+
def embed_text(text):
|
| 42 |
try:
|
| 43 |
+
return genai.embed_content(
|
| 44 |
+
model='models/embedding-001',
|
| 45 |
+
content=text,
|
| 46 |
+
task_type='retrieval_document'
|
| 47 |
+
)['embedding']
|
| 48 |
except Exception as e:
|
| 49 |
+
raise RuntimeError(f"Error embedding text: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# Add embeddings to the DataFrame
|
| 52 |
if 'Embeddings' not in df.columns:
|
| 53 |
df['Embeddings'] = df['Text'].apply(embed_text)
|
| 54 |
|
|
|
|
| 59 |
|
| 60 |
# Function to get the most similar document based on the query
|
| 61 |
def most_similar_document(query):
|
|
|
|
| 62 |
local_df = df.copy()
|
|
|
|
| 63 |
local_df['Similarity'] = local_df['Embeddings'].apply(lambda vector: query_similarity_score(query, vector))
|
|
|
|
| 64 |
most_similar = local_df.sort_values('Similarity', ascending=False).iloc[0]
|
| 65 |
return most_similar['Title'], most_similar['Text']
|
| 66 |
|
| 67 |
# Function to generate a response using the RAG approach
|
| 68 |
def RAG(query):
|
| 69 |
+
try:
|
| 70 |
+
title, text = most_similar_document(query)
|
| 71 |
+
model = genai.GenerativeModel('gemini-pro')
|
| 72 |
+
prompt = f"Answer this query:\n{query}.\nOnly use this context to answer:\n{text}"
|
| 73 |
+
response = model.generate_content(prompt)
|
| 74 |
+
return f"{response.text}\n\nSource Document: {title}"
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return f"Error: {e}"
|
| 77 |
|
| 78 |
+
# Gradio interface
|
| 79 |
iface = gr.Interface(
|
| 80 |
fn=RAG, # Main function to handle the query
|
| 81 |
inputs=[
|
| 82 |
gr.Textbox(label="Enter Your Query"), # Input for the user's query
|
| 83 |
],
|
| 84 |
outputs=gr.Textbox(label="Response"), # Output for the generated response
|
| 85 |
+
title="Patrick's Multilingual Query Handler"
|
| 86 |
)
|
| 87 |
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
iface.launch()
|