Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import os
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
|
7 |
+
# Page configuration
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="Title-2-BookName Converter",
|
10 |
+
page_icon="📚",
|
11 |
+
layout="centered",
|
12 |
+
initial_sidebar_state="collapsed"
|
13 |
+
)
|
14 |
+
|
15 |
+
# Custom CSS for better styling
|
16 |
+
st.markdown("""
|
17 |
+
<style>
|
18 |
+
.main {
|
19 |
+
padding: 2rem;
|
20 |
+
}
|
21 |
+
.stApp {
|
22 |
+
background-color: #f5f7fa;
|
23 |
+
}
|
24 |
+
.stTitle {
|
25 |
+
font-size: 3rem !important;
|
26 |
+
margin-bottom: 1.5rem !important;
|
27 |
+
}
|
28 |
+
.search-container {
|
29 |
+
background-color: white;
|
30 |
+
padding: 2rem;
|
31 |
+
border-radius: 10px;
|
32 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
33 |
+
}
|
34 |
+
.results-container {
|
35 |
+
margin-top: 2rem;
|
36 |
+
background-color: white;
|
37 |
+
padding: 2rem;
|
38 |
+
border-radius: 10px;
|
39 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
40 |
+
}
|
41 |
+
.st-emotion-cache-1n76uvr {
|
42 |
+
font-size: 1.2rem;
|
43 |
+
}
|
44 |
+
</style>
|
45 |
+
""", unsafe_allow_html=True)
|
46 |
+
|
47 |
+
# Load the CSV file with caching
|
48 |
+
@st.cache_data
|
49 |
+
def load_data():
|
50 |
+
try:
|
51 |
+
HUGGINGFACE_TOKEN = os.getenv("HF_TOKEN")
|
52 |
+
if not HUGGINGFACE_TOKEN:
|
53 |
+
st.sidebar.error("HF_TOKEN environment variable is not set")
|
54 |
+
return pd.DataFrame()
|
55 |
+
|
56 |
+
csv_file = hf_hub_download(
|
57 |
+
repo_id="Skoob/test-private",
|
58 |
+
filename="2025Feb_3_Data_2k_267k.csv",
|
59 |
+
use_auth_token=HUGGINGFACE_TOKEN
|
60 |
+
)
|
61 |
+
df = pd.read_csv(csv_file)
|
62 |
+
# Prepare dataframe
|
63 |
+
df = df.copy()
|
64 |
+
return df
|
65 |
+
except Exception as e:
|
66 |
+
st.sidebar.error(f"Error loading data: {str(e)}")
|
67 |
+
return pd.DataFrame()
|
68 |
+
|
69 |
+
st.title(":blue[Title-2-BookName]")
|
70 |
+
|
71 |
+
# Load data
|
72 |
+
title_df = load_data()
|
73 |
+
|
74 |
+
# Main search functionality
|
75 |
+
def get_file_info(user_title):
|
76 |
+
if title_df.empty:
|
77 |
+
return None
|
78 |
+
|
79 |
+
# Always case-insensitive search with exact match only
|
80 |
+
search_title = user_title.lower()
|
81 |
+
result = title_df.loc[title_df['Title'].str.lower() == search_title]
|
82 |
+
|
83 |
+
if not result.empty:
|
84 |
+
# Remove 'BookSummary-' and '.docx' from the file names
|
85 |
+
result_copy = result.copy()
|
86 |
+
result_copy['Clean_name'] = result_copy['File_name'].str.replace("BookSummary-", "").str.replace(".docx", "")
|
87 |
+
# Select relevant columns and reset index
|
88 |
+
return result_copy[['Title', 'Clean_name', 'File_name']].reset_index(drop=True)
|
89 |
+
else:
|
90 |
+
return None
|
91 |
+
|
92 |
+
# Main app container
|
93 |
+
st.markdown('<div class="search-container">', unsafe_allow_html=True)
|
94 |
+
|
95 |
+
# Enhanced search interface
|
96 |
+
col1, col2 = st.columns([4, 1])
|
97 |
+
with col1:
|
98 |
+
user_title = st.text_input("Enter the book title:")
|
99 |
+
with col2:
|
100 |
+
search_button = st.button("🔍 Search", use_container_width=True)
|
101 |
+
|
102 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
103 |
+
|
104 |
+
# Search results display
|
105 |
+
if user_title and search_button:
|
106 |
+
st.markdown('<div class="results-container">', unsafe_allow_html=True)
|
107 |
+
|
108 |
+
with st.spinner("Searching..."):
|
109 |
+
result_df = get_file_info(user_title)
|
110 |
+
|
111 |
+
if result_df is not None and not result_df.empty:
|
112 |
+
st.success(f"Found {len(result_df)} matching result(s)!")
|
113 |
+
|
114 |
+
# Display results in a nice table with copy buttons
|
115 |
+
st.dataframe(
|
116 |
+
result_df,
|
117 |
+
column_config={
|
118 |
+
"Title": st.column_config.TextColumn("Original Title"),
|
119 |
+
"Clean_name": st.column_config.TextColumn("Book Name"),
|
120 |
+
"File_name": st.column_config.TextColumn("File Name")
|
121 |
+
},
|
122 |
+
use_container_width=True,
|
123 |
+
hide_index=True
|
124 |
+
)
|
125 |
+
|
126 |
+
# Display all file names in a list format
|
127 |
+
if len(result_df) >= 1:
|
128 |
+
st.subheader("All File Names:")
|
129 |
+
for idx, row in result_df.iterrows():
|
130 |
+
st.code(row['File_name'], language=None)
|
131 |
+
|
132 |
+
# Add option to copy all file names as text
|
133 |
+
all_filenames = '\n'.join(result_df['File_name'].tolist())
|
134 |
+
st.download_button(
|
135 |
+
label="📋 Copy all file names",
|
136 |
+
data=all_filenames,
|
137 |
+
file_name=f"filenames_{user_title}.txt",
|
138 |
+
mime="text/plain"
|
139 |
+
)
|
140 |
+
else:
|
141 |
+
st.warning("No results found. Try different search options or check your spelling.")
|