Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -70,7 +70,7 @@ def get_vector_store(chunks):
|
|
| 70 |
"""從文字區塊創建並保存FAISS向量存儲"""
|
| 71 |
try:
|
| 72 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 73 |
-
model="models/embedding-
|
| 74 |
google_api_key=gemini_api_key
|
| 75 |
)
|
| 76 |
|
|
@@ -82,7 +82,7 @@ def get_vector_store(chunks):
|
|
| 82 |
return False
|
| 83 |
|
| 84 |
def get_conversational_chain():
|
| 85 |
-
"""Create the conversational chain for Q&A"""
|
| 86 |
prompt_template = """
|
| 87 |
Answer the question as detailed as possible from the provided context. Make sure to provide all the details.
|
| 88 |
If you need more details to perfectly answer the question, then ask for more details that you think need to be known.
|
|
@@ -94,10 +94,14 @@ def get_conversational_chain():
|
|
| 94 |
Answer:
|
| 95 |
"""
|
| 96 |
|
|
|
|
| 97 |
model = ChatGoogleGenerativeAI(
|
| 98 |
-
model="gemini-
|
| 99 |
google_api_key=gemini_api_key,
|
| 100 |
-
temperature=0.3
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
prompt = PromptTemplate(
|
|
@@ -116,9 +120,9 @@ def handle_user_input(question):
|
|
| 116 |
st.warning("Please upload and process PDF files first!")
|
| 117 |
return
|
| 118 |
|
| 119 |
-
# Load the vector store
|
| 120 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 121 |
-
model="models/embedding-
|
| 122 |
google_api_key=gemini_api_key
|
| 123 |
)
|
| 124 |
|
|
@@ -128,8 +132,8 @@ def handle_user_input(question):
|
|
| 128 |
allow_dangerous_deserialization=True
|
| 129 |
)
|
| 130 |
|
| 131 |
-
# Search for similar documents
|
| 132 |
-
docs = vector_store.similarity_search(question, k=
|
| 133 |
|
| 134 |
if not docs:
|
| 135 |
st.write("No relevant information found in the uploaded documents.")
|
|
@@ -145,7 +149,7 @@ def handle_user_input(question):
|
|
| 145 |
return_only_outputs=True
|
| 146 |
)
|
| 147 |
|
| 148 |
-
st.write("**Reply:**")
|
| 149 |
st.write(response["output_text"])
|
| 150 |
|
| 151 |
except Exception as e:
|
|
@@ -154,13 +158,20 @@ def handle_user_input(question):
|
|
| 154 |
def main():
|
| 155 |
"""Main Streamlit application"""
|
| 156 |
st.set_page_config(
|
| 157 |
-
page_title="Chat with Multiple PDFs",
|
| 158 |
-
page_icon="
|
| 159 |
layout="wide"
|
| 160 |
)
|
| 161 |
|
| 162 |
-
st.header("
|
| 163 |
-
st.markdown("Upload your PDF files and ask questions about their content!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
# Create two columns for better layout
|
| 166 |
col1, col2 = st.columns([2, 1])
|
|
@@ -173,7 +184,7 @@ def main():
|
|
| 173 |
)
|
| 174 |
|
| 175 |
if user_question:
|
| 176 |
-
with st.spinner("
|
| 177 |
handle_user_input(user_question)
|
| 178 |
|
| 179 |
with col2:
|
|
@@ -190,7 +201,7 @@ def main():
|
|
| 190 |
st.success(f"✅ {len(pdf_docs)} PDF file(s) uploaded")
|
| 191 |
|
| 192 |
if st.button("🔄 Process PDFs", type="primary"):
|
| 193 |
-
with st.spinner("Processing PDFs..."):
|
| 194 |
progress_bar = st.progress(0)
|
| 195 |
|
| 196 |
# Extract text from all PDFs
|
|
@@ -224,15 +235,25 @@ def main():
|
|
| 224 |
1. **Upload PDFs**: Click 'Choose PDF files' and select one or more PDF files
|
| 225 |
2. **Process**: Click 'Process PDFs' to analyze your documents
|
| 226 |
3. **Ask Questions**: Type your questions in the search box
|
| 227 |
-
4. **Get Answers**:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
""")
|
| 229 |
|
| 230 |
-
st.markdown("### 🔧 Features:")
|
| 231 |
st.markdown("""
|
| 232 |
- ✅ Multiple PDF support
|
| 233 |
-
- 🤖 AI-powered Q&A with
|
| 234 |
-
- 🔍
|
| 235 |
-
- 📊
|
|
|
|
| 236 |
""")
|
| 237 |
|
| 238 |
if os.path.exists("faiss_index"):
|
|
|
|
| 70 |
"""從文字區塊創建並保存FAISS向量存儲"""
|
| 71 |
try:
|
| 72 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 73 |
+
model="models/text-embedding-004", # Updated to newer embedding model
|
| 74 |
google_api_key=gemini_api_key
|
| 75 |
)
|
| 76 |
|
|
|
|
| 82 |
return False
|
| 83 |
|
| 84 |
def get_conversational_chain():
|
| 85 |
+
"""Create the conversational chain for Q&A with Flash 2.0"""
|
| 86 |
prompt_template = """
|
| 87 |
Answer the question as detailed as possible from the provided context. Make sure to provide all the details.
|
| 88 |
If you need more details to perfectly answer the question, then ask for more details that you think need to be known.
|
|
|
|
| 94 |
Answer:
|
| 95 |
"""
|
| 96 |
|
| 97 |
+
# Using Flash 2.0 model
|
| 98 |
model = ChatGoogleGenerativeAI(
|
| 99 |
+
model="gemini-2.0-flash-exp", # Flash 2.0 model
|
| 100 |
google_api_key=gemini_api_key,
|
| 101 |
+
temperature=0.3,
|
| 102 |
+
max_tokens=8192, # Flash 2.0 supports larger context
|
| 103 |
+
top_p=0.8,
|
| 104 |
+
top_k=40
|
| 105 |
)
|
| 106 |
|
| 107 |
prompt = PromptTemplate(
|
|
|
|
| 120 |
st.warning("Please upload and process PDF files first!")
|
| 121 |
return
|
| 122 |
|
| 123 |
+
# Load the vector store with updated embedding model
|
| 124 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 125 |
+
model="models/text-embedding-004", # Updated to newer embedding model
|
| 126 |
google_api_key=gemini_api_key
|
| 127 |
)
|
| 128 |
|
|
|
|
| 132 |
allow_dangerous_deserialization=True
|
| 133 |
)
|
| 134 |
|
| 135 |
+
# Search for similar documents (increased k for Flash 2.0's better context handling)
|
| 136 |
+
docs = vector_store.similarity_search(question, k=6)
|
| 137 |
|
| 138 |
if not docs:
|
| 139 |
st.write("No relevant information found in the uploaded documents.")
|
|
|
|
| 149 |
return_only_outputs=True
|
| 150 |
)
|
| 151 |
|
| 152 |
+
st.write("**Reply (Flash 2.0):**")
|
| 153 |
st.write(response["output_text"])
|
| 154 |
|
| 155 |
except Exception as e:
|
|
|
|
| 158 |
def main():
|
| 159 |
"""Main Streamlit application"""
|
| 160 |
st.set_page_config(
|
| 161 |
+
page_title="Chat with Multiple PDFs - Flash 2.0",
|
| 162 |
+
page_icon="⚡",
|
| 163 |
layout="wide"
|
| 164 |
)
|
| 165 |
|
| 166 |
+
st.header("⚡ Chat With Multiple PDFs using Flash 2.0")
|
| 167 |
+
st.markdown("Upload your PDF files and ask questions about their content using Google's latest Flash 2.0 model!")
|
| 168 |
+
|
| 169 |
+
# Model info badge
|
| 170 |
+
st.markdown("""
|
| 171 |
+
<div style="background-color: #e8f4f8; padding: 10px; border-radius: 5px; margin-bottom: 20px;">
|
| 172 |
+
<strong>🚀 Powered by Flash 2.0</strong> - Google's fastest and most efficient model with enhanced reasoning capabilities
|
| 173 |
+
</div>
|
| 174 |
+
""", unsafe_allow_html=True)
|
| 175 |
|
| 176 |
# Create two columns for better layout
|
| 177 |
col1, col2 = st.columns([2, 1])
|
|
|
|
| 184 |
)
|
| 185 |
|
| 186 |
if user_question:
|
| 187 |
+
with st.spinner("Flash 2.0 is processing your question..."):
|
| 188 |
handle_user_input(user_question)
|
| 189 |
|
| 190 |
with col2:
|
|
|
|
| 201 |
st.success(f"✅ {len(pdf_docs)} PDF file(s) uploaded")
|
| 202 |
|
| 203 |
if st.button("🔄 Process PDFs", type="primary"):
|
| 204 |
+
with st.spinner("Processing PDFs with Flash 2.0..."):
|
| 205 |
progress_bar = st.progress(0)
|
| 206 |
|
| 207 |
# Extract text from all PDFs
|
|
|
|
| 235 |
1. **Upload PDFs**: Click 'Choose PDF files' and select one or more PDF files
|
| 236 |
2. **Process**: Click 'Process PDFs' to analyze your documents
|
| 237 |
3. **Ask Questions**: Type your questions in the search box
|
| 238 |
+
4. **Get Answers**: Flash 2.0 will provide fast, accurate answers based on your documents
|
| 239 |
+
""")
|
| 240 |
+
|
| 241 |
+
st.markdown("### ⚡ Flash 2.0 Features:")
|
| 242 |
+
st.markdown("""
|
| 243 |
+
- ⚡ **Ultra-fast responses** - 2x faster than Gemini Pro
|
| 244 |
+
- 🧠 **Enhanced reasoning** - Better understanding of complex queries
|
| 245 |
+
- 📈 **Improved accuracy** - More precise answers from documents
|
| 246 |
+
- 🔄 **Better context handling** - Processes more relevant information
|
| 247 |
+
- 💰 **Cost efficient** - Lower API costs per query
|
| 248 |
""")
|
| 249 |
|
| 250 |
+
st.markdown("### 🔧 Technical Features:")
|
| 251 |
st.markdown("""
|
| 252 |
- ✅ Multiple PDF support
|
| 253 |
+
- 🤖 AI-powered Q&A with Flash 2.0
|
| 254 |
+
- 🔍 Advanced semantic search
|
| 255 |
+
- 📊 Optimized text chunking
|
| 256 |
+
- 🎯 Improved embedding model (text-embedding-004)
|
| 257 |
""")
|
| 258 |
|
| 259 |
if os.path.exists("faiss_index"):
|