Shami96 commited on
Commit
11fadc8
·
verified ·
1 Parent(s): 89685e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -29
app.py CHANGED
@@ -18,37 +18,68 @@ with tab1:
18
 
19
  if uploaded_file and user_name:
20
  try:
21
- file_text = uploaded_file.read().decode("utf-8", errors="ignore")
 
22
 
23
- # Create a concise summary request
24
- summary_prompt = f"This is a government tender document. Create a very brief summary in 100 words or less."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # Process the document in smaller chunks
27
- with st.spinner("Analyzing the tender..."):
28
- # First get a high-level summary of the document
29
- chunks = chunk_text(file_text, max_tokens=3000)
30
- summary = ""
31
-
32
- # Process first chunk to get a summary
33
- if chunks:
34
- summary = llm(f"{chunks[0]}\n\n{summary_prompt}")
35
-
36
- # Now ask targeted questions about the summary
37
- analysis_prompt = f"""
38
- Based on this tender summary: "{summary}"
39
-
40
- Please provide the following information for {user_name}:
41
- 1. What is this tender for? (1-2 sentences)
42
- 2. What are the key requirements? (3-4 points)
43
- 3. Who can apply? (1-2 sentences)
44
- 4. What are important deadlines? (if mentioned)
45
-
46
- Keep your response concise and easy to understand.
47
- """
48
- detailed_analysis = llm(analysis_prompt)
49
-
50
- st.success("Tender Summary:")
51
- st.write(detailed_analysis)
 
 
 
 
 
 
 
 
 
 
 
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.")