Spaces:
Sleeping
Sleeping
Update view_ppt.py
Browse files- view_ppt.py +111 -111
view_ppt.py
CHANGED
|
@@ -1,111 +1,111 @@
|
|
| 1 |
-
#TAB : VIEW PPT.py
|
| 2 |
-
import requests
|
| 3 |
-
from sentence_transformers import SentenceTransformer, CrossEncoder
|
| 4 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
-
import os
|
| 6 |
-
import shutil
|
| 7 |
-
|
| 8 |
-
# Local cache directory for downloaded files
|
| 9 |
-
LOCAL_CACHE_DIR = "local_cache"
|
| 10 |
-
os.makedirs(LOCAL_CACHE_DIR, exist_ok=True)
|
| 11 |
-
|
| 12 |
-
# Function to download a file from OneDrive to the local cache
|
| 13 |
-
def download_file_from_onedrive(file_path, file_id, headers):
|
| 14 |
-
local_file_path = os.path.join(LOCAL_CACHE_DIR, os.path.basename(file_path))
|
| 15 |
-
|
| 16 |
-
if not os.path.exists(local_file_path): # Avoid re-downloading
|
| 17 |
-
download_url = f"https://graph.microsoft.com/v1.0/me/drive/items/{file_id}/content"
|
| 18 |
-
response = requests.get(download_url, headers=headers)
|
| 19 |
-
|
| 20 |
-
if response.status_code != 200:
|
| 21 |
-
raise ValueError(f"Failed to download file {file_path}. Error: {response.text}")
|
| 22 |
-
|
| 23 |
-
with open(local_file_path, "wb") as f:
|
| 24 |
-
f.write(response.content)
|
| 25 |
-
|
| 26 |
-
print(f"✅ Downloaded: {file_path} -> {local_file_path}")
|
| 27 |
-
|
| 28 |
-
return local_file_path
|
| 29 |
-
|
| 30 |
-
# Function to search PPTs
|
| 31 |
-
def search_ppts(query, num_results):
|
| 32 |
-
|
| 33 |
-
global df
|
| 34 |
-
gr.Info("Searching the relevant PPTs .")
|
| 35 |
-
# Generate query embedding
|
| 36 |
-
query_embedding = embedding_model.encode(query).tolist()
|
| 37 |
-
# Filter the DataFrame to include only rows where Unique_Slide_ID ends with "slide_1"
|
| 38 |
-
df1 = df[df['Unique_Slide_ID'].str.endswith("slide_1", na=False)]
|
| 39 |
-
# Compute cosine similarity scores
|
| 40 |
-
df1['similarity'] = df1['Short_Summary_Embedding'].apply(
|
| 41 |
-
lambda x: cosine_similarity([query_embedding], [eval(x)])[0][0]
|
| 42 |
-
)
|
| 43 |
-
|
| 44 |
-
# Sort by cosine similarity score
|
| 45 |
-
df1 = df1.sort_values(by='similarity', ascending=False)
|
| 46 |
-
|
| 47 |
-
# Get top N results for reranking
|
| 48 |
-
top_n = min(50, len(df1)) # Take top 50 results for reranking
|
| 49 |
-
top_results = df1.head(top_n)
|
| 50 |
-
|
| 51 |
-
# Prepare input pairs for cross-encoder reranking
|
| 52 |
-
pairs = [(query, row['Short_Summary']) for _, row in top_results.iterrows()]
|
| 53 |
-
|
| 54 |
-
# Rerank using cross-encoder
|
| 55 |
-
gr.Info("Doing Semantic Reranking for most appropriate results ")
|
| 56 |
-
rerank_scores = cross_encoder.predict(pairs)
|
| 57 |
-
top_results = top_results.copy() # Avoid SettingWithCopyWarning
|
| 58 |
-
top_results['rerank_score'] = rerank_scores
|
| 59 |
-
|
| 60 |
-
# Sort by rerank score
|
| 61 |
-
top_results = top_results.sort_values(by='rerank_score', ascending=False)
|
| 62 |
-
print(top_results)
|
| 63 |
-
# Prepare results
|
| 64 |
-
results = []
|
| 65 |
-
gr.Info('Downloading PPT images and ppt')
|
| 66 |
-
print('Downloading PPT images and ppt')
|
| 67 |
-
for _, row in top_results.head(num_results).iterrows():
|
| 68 |
-
|
| 69 |
-
# Download slide image locally
|
| 70 |
-
slide_image_path = download_file_from_onedrive(
|
| 71 |
-
row['Thumbnail_File_Path'], row['Thumbnail_File_ID'], headers
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
# Download full PPT locally
|
| 75 |
-
ppt_download_link = download_file_from_onedrive(
|
| 76 |
-
row['Full_PPT_File_Path'], row['Full_PPT_File_ID'], headers
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
title = row['Suitable_Title']
|
| 80 |
-
owner = row['PPT_Owner']
|
| 81 |
-
category = row['Slide_Category']
|
| 82 |
-
summary = row['Short_Summary']
|
| 83 |
-
|
| 84 |
-
results.append({
|
| 85 |
-
"image": slide_image_path,
|
| 86 |
-
"title": title,
|
| 87 |
-
"owner": owner,
|
| 88 |
-
"category": category,
|
| 89 |
-
"summary": summary,
|
| 90 |
-
"download_link": ppt_download_link
|
| 91 |
-
})
|
| 92 |
-
print("downloading complete ")
|
| 93 |
-
# Update visibility of rows
|
| 94 |
-
visible_rows = min(len(results), num_results)
|
| 95 |
-
row_updates = []
|
| 96 |
-
row_updates = []
|
| 97 |
-
for i in range(20):
|
| 98 |
-
if i < len(results):
|
| 99 |
-
result = results[i]
|
| 100 |
-
row_updates.extend([
|
| 101 |
-
gr.update(visible=True), # ✅ Make the row visible
|
| 102 |
-
gr.update(value=result["image"], visible=True),
|
| 103 |
-
gr.update(value=f"<b>Title:</b> {result['title']}<br><b>Owner:</b> {result['owner']}<br><b>Category:</b> {result['category']}", visible=True),
|
| 104 |
-
gr.update(value=result["summary"], visible=True),
|
| 105 |
-
gr.update(value=result["download_link"], visible=True),
|
| 106 |
-
])
|
| 107 |
-
else:
|
| 108 |
-
row_updates.extend([gr.update(visible=False)] * 5) # row + 4 components
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
return row_updates
|
|
|
|
| 1 |
+
#TAB : VIEW PPT.py
|
| 2 |
+
import requests
|
| 3 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder
|
| 4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
+
import os
|
| 6 |
+
import shutil
|
| 7 |
+
import gradio as gr
|
| 8 |
+
# Local cache directory for downloaded files
|
| 9 |
+
LOCAL_CACHE_DIR = "local_cache"
|
| 10 |
+
os.makedirs(LOCAL_CACHE_DIR, exist_ok=True)
|
| 11 |
+
|
| 12 |
+
# Function to download a file from OneDrive to the local cache
|
| 13 |
+
def download_file_from_onedrive(file_path, file_id, headers):
|
| 14 |
+
local_file_path = os.path.join(LOCAL_CACHE_DIR, os.path.basename(file_path))
|
| 15 |
+
|
| 16 |
+
if not os.path.exists(local_file_path): # Avoid re-downloading
|
| 17 |
+
download_url = f"https://graph.microsoft.com/v1.0/me/drive/items/{file_id}/content"
|
| 18 |
+
response = requests.get(download_url, headers=headers)
|
| 19 |
+
|
| 20 |
+
if response.status_code != 200:
|
| 21 |
+
raise ValueError(f"Failed to download file {file_path}. Error: {response.text}")
|
| 22 |
+
|
| 23 |
+
with open(local_file_path, "wb") as f:
|
| 24 |
+
f.write(response.content)
|
| 25 |
+
|
| 26 |
+
print(f"✅ Downloaded: {file_path} -> {local_file_path}")
|
| 27 |
+
|
| 28 |
+
return local_file_path
|
| 29 |
+
|
| 30 |
+
# Function to search PPTs
|
| 31 |
+
def search_ppts(query, num_results):
|
| 32 |
+
|
| 33 |
+
global df
|
| 34 |
+
gr.Info("Searching the relevant PPTs .")
|
| 35 |
+
# Generate query embedding
|
| 36 |
+
query_embedding = embedding_model.encode(query).tolist()
|
| 37 |
+
# Filter the DataFrame to include only rows where Unique_Slide_ID ends with "slide_1"
|
| 38 |
+
df1 = df[df['Unique_Slide_ID'].str.endswith("slide_1", na=False)]
|
| 39 |
+
# Compute cosine similarity scores
|
| 40 |
+
df1['similarity'] = df1['Short_Summary_Embedding'].apply(
|
| 41 |
+
lambda x: cosine_similarity([query_embedding], [eval(x)])[0][0]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
# Sort by cosine similarity score
|
| 45 |
+
df1 = df1.sort_values(by='similarity', ascending=False)
|
| 46 |
+
|
| 47 |
+
# Get top N results for reranking
|
| 48 |
+
top_n = min(50, len(df1)) # Take top 50 results for reranking
|
| 49 |
+
top_results = df1.head(top_n)
|
| 50 |
+
|
| 51 |
+
# Prepare input pairs for cross-encoder reranking
|
| 52 |
+
pairs = [(query, row['Short_Summary']) for _, row in top_results.iterrows()]
|
| 53 |
+
|
| 54 |
+
# Rerank using cross-encoder
|
| 55 |
+
gr.Info("Doing Semantic Reranking for most appropriate results ")
|
| 56 |
+
rerank_scores = cross_encoder.predict(pairs)
|
| 57 |
+
top_results = top_results.copy() # Avoid SettingWithCopyWarning
|
| 58 |
+
top_results['rerank_score'] = rerank_scores
|
| 59 |
+
|
| 60 |
+
# Sort by rerank score
|
| 61 |
+
top_results = top_results.sort_values(by='rerank_score', ascending=False)
|
| 62 |
+
print(top_results)
|
| 63 |
+
# Prepare results
|
| 64 |
+
results = []
|
| 65 |
+
gr.Info('Downloading PPT images and ppt')
|
| 66 |
+
print('Downloading PPT images and ppt')
|
| 67 |
+
for _, row in top_results.head(num_results).iterrows():
|
| 68 |
+
|
| 69 |
+
# Download slide image locally
|
| 70 |
+
slide_image_path = download_file_from_onedrive(
|
| 71 |
+
row['Thumbnail_File_Path'], row['Thumbnail_File_ID'], headers
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Download full PPT locally
|
| 75 |
+
ppt_download_link = download_file_from_onedrive(
|
| 76 |
+
row['Full_PPT_File_Path'], row['Full_PPT_File_ID'], headers
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
title = row['Suitable_Title']
|
| 80 |
+
owner = row['PPT_Owner']
|
| 81 |
+
category = row['Slide_Category']
|
| 82 |
+
summary = row['Short_Summary']
|
| 83 |
+
|
| 84 |
+
results.append({
|
| 85 |
+
"image": slide_image_path,
|
| 86 |
+
"title": title,
|
| 87 |
+
"owner": owner,
|
| 88 |
+
"category": category,
|
| 89 |
+
"summary": summary,
|
| 90 |
+
"download_link": ppt_download_link
|
| 91 |
+
})
|
| 92 |
+
print("downloading complete ")
|
| 93 |
+
# Update visibility of rows
|
| 94 |
+
visible_rows = min(len(results), num_results)
|
| 95 |
+
row_updates = []
|
| 96 |
+
row_updates = []
|
| 97 |
+
for i in range(20):
|
| 98 |
+
if i < len(results):
|
| 99 |
+
result = results[i]
|
| 100 |
+
row_updates.extend([
|
| 101 |
+
gr.update(visible=True), # ✅ Make the row visible
|
| 102 |
+
gr.update(value=result["image"], visible=True),
|
| 103 |
+
gr.update(value=f"<b>Title:</b> {result['title']}<br><b>Owner:</b> {result['owner']}<br><b>Category:</b> {result['category']}", visible=True),
|
| 104 |
+
gr.update(value=result["summary"], visible=True),
|
| 105 |
+
gr.update(value=result["download_link"], visible=True),
|
| 106 |
+
])
|
| 107 |
+
else:
|
| 108 |
+
row_updates.extend([gr.update(visible=False)] * 5) # row + 4 components
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
return row_updates
|