Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,37 +18,68 @@ with tab1:
|
|
| 18 |
|
| 19 |
if uploaded_file and user_name:
|
| 20 |
try:
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
with st.
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
st.error(f"Error processing file: {str(e)}")
|
| 54 |
st.info("Try uploading a smaller file or a text extract of the most important sections.")
|
|
|
|
| 18 |
|
| 19 |
if uploaded_file and user_name:
|
| 20 |
try:
|
| 21 |
+
# Process different file types
|
| 22 |
+
file_extension = uploaded_file.name.split('.')[-1].lower()
|
| 23 |
|
| 24 |
+
if file_extension == 'pdf':
|
| 25 |
+
# Handle PDF files
|
| 26 |
+
try:
|
| 27 |
+
import pdfplumber
|
| 28 |
+
pdf = pdfplumber.open(uploaded_file)
|
| 29 |
+
file_text = ""
|
| 30 |
+
# Extract text from first few pages
|
| 31 |
+
for i in range(min(5, len(pdf.pages))):
|
| 32 |
+
page_text = pdf.pages[i].extract_text() or ""
|
| 33 |
+
file_text += page_text + "\n\n"
|
| 34 |
+
pdf.close()
|
| 35 |
+
except Exception as e:
|
| 36 |
+
st.error(f"Error processing PDF: {str(e)}")
|
| 37 |
+
file_text = "Error extracting PDF content"
|
| 38 |
+
else:
|
| 39 |
+
# Handle text files
|
| 40 |
+
file_content = uploaded_file.read()
|
| 41 |
+
try:
|
| 42 |
+
file_text = file_content.decode("utf-8", errors="ignore")
|
| 43 |
+
except:
|
| 44 |
+
file_text = str(file_content)
|
| 45 |
|
| 46 |
+
# Show a preview of the document (first 500 chars)
|
| 47 |
+
with st.expander("Document Preview"):
|
| 48 |
+
st.text(file_text[:500] + "..." if len(file_text) > 500 else file_text)
|
| 49 |
+
|
| 50 |
+
# Process the document if we have content
|
| 51 |
+
if file_text:
|
| 52 |
+
# Create direct prompt with the document content
|
| 53 |
+
with st.spinner("Analyzing the tender document..."):
|
| 54 |
+
# Chunk the text if needed
|
| 55 |
+
chunks = chunk_text(file_text, max_tokens=3000)
|
| 56 |
+
|
| 57 |
+
if not chunks:
|
| 58 |
+
st.error("Unable to process document - no valid content found")
|
| 59 |
+
else:
|
| 60 |
+
# Direct analysis of first chunk
|
| 61 |
+
analysis_prompt = f"""
|
| 62 |
+
The following is a government tender document:
|
| 63 |
+
|
| 64 |
+
{chunks[0]}
|
| 65 |
+
|
| 66 |
+
Please analyze this tender document and provide a clear, simple explanation for {user_name}:
|
| 67 |
+
|
| 68 |
+
1. What is this tender for? (explain the purpose clearly)
|
| 69 |
+
2. What are the key requirements to apply?
|
| 70 |
+
3. Who is eligible to apply for this tender?
|
| 71 |
+
4. What are the important deadlines?
|
| 72 |
+
5. What documents or qualifications are needed?
|
| 73 |
+
|
| 74 |
+
Address {user_name} directly in your response. Make it personal.
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
detailed_analysis = llm(analysis_prompt)
|
| 78 |
+
|
| 79 |
+
st.success("Tender Summary:")
|
| 80 |
+
st.write(detailed_analysis)
|
| 81 |
+
else:
|
| 82 |
+
st.error("Could not extract text from the uploaded file")
|
| 83 |
except Exception as e:
|
| 84 |
st.error(f"Error processing file: {str(e)}")
|
| 85 |
st.info("Try uploading a smaller file or a text extract of the most important sections.")
|