Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from utils import fetch_active_tenders, fetch_tender_details, get_ppra_resources
|
| 3 |
from model_config import get_model
|
| 4 |
|
| 5 |
st.set_page_config(page_title="PPRA Co-Pilot", layout="wide")
|
|
@@ -15,40 +15,103 @@ with tab1:
|
|
| 15 |
st.subheader("Upload a Tender Document (PDF or Text)")
|
| 16 |
uploaded_file = st.file_uploader("Choose a file", type=["pdf", "txt"])
|
| 17 |
user_name = st.text_input("Enter your name")
|
| 18 |
-
|
| 19 |
if uploaded_file and user_name:
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
st.success("Tender Summary:")
|
| 25 |
-
st.write(
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# View Active Tenders Tab
|
| 28 |
with tab2:
|
| 29 |
st.subheader("π Active Government Tenders")
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
st.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# PPRA Resources Tab
|
| 49 |
with tab3:
|
| 50 |
st.header("π PPRA Official Resources")
|
| 51 |
st.markdown("Below are official links to all relevant PPRA documentation and tender policies.")
|
| 52 |
resources = get_ppra_resources()
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from utils import fetch_active_tenders, fetch_tender_details, get_ppra_resources, chunk_text
|
| 3 |
from model_config import get_model
|
| 4 |
|
| 5 |
st.set_page_config(page_title="PPRA Co-Pilot", layout="wide")
|
|
|
|
| 15 |
st.subheader("Upload a Tender Document (PDF or Text)")
|
| 16 |
uploaded_file = st.file_uploader("Choose a file", type=["pdf", "txt"])
|
| 17 |
user_name = st.text_input("Enter your name")
|
| 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.")
|
| 55 |
|
| 56 |
# View Active Tenders Tab
|
| 57 |
with tab2:
|
| 58 |
st.subheader("π Active Government Tenders")
|
| 59 |
+
|
| 60 |
+
# Add a refresh button
|
| 61 |
+
if st.button("π Refresh Tenders"):
|
| 62 |
+
st.experimental_rerun()
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
with st.spinner("Fetching active tenders..."):
|
| 66 |
+
tenders = fetch_active_tenders()
|
| 67 |
+
|
| 68 |
+
if not tenders or len(tenders) == 0:
|
| 69 |
+
st.warning("No active tenders found or there was an issue fetching the tenders.")
|
| 70 |
+
else:
|
| 71 |
+
tender_titles = [t["title"] for t in tenders]
|
| 72 |
+
selected = st.selectbox("Select a tender to understand", tender_titles)
|
| 73 |
+
|
| 74 |
+
if selected:
|
| 75 |
+
tender = next((t for t in tenders if t["title"] == selected), None)
|
| 76 |
+
if tender:
|
| 77 |
+
st.markdown(f"### π {tender['title']}")
|
| 78 |
+
st.markdown(f"**Department:** {tender['department']}")
|
| 79 |
+
st.markdown(f"**Closing Date:** {tender['closing_date']}")
|
| 80 |
+
st.markdown(f"[View Tender Detail]({tender['link']})")
|
| 81 |
+
|
| 82 |
+
if st.button("π§ Summarize and Explain This Tender"):
|
| 83 |
+
with st.spinner("Fetching tender details..."):
|
| 84 |
+
content = fetch_tender_details(tender['link'])
|
| 85 |
+
|
| 86 |
+
if content:
|
| 87 |
+
prompt = f"This is a government tender document from {tender['department']}. The title is: {tender['title']}.\n\nHere's some content from the tender:\n\n{content}\n\nExplain this tender for a Pakistani citizen. What are the key requirements? Keep your response brief and focus on practical information."
|
| 88 |
+
|
| 89 |
+
with st.spinner("Generating explanation..."):
|
| 90 |
+
summary = llm(prompt)
|
| 91 |
+
|
| 92 |
+
st.write(summary)
|
| 93 |
+
else:
|
| 94 |
+
st.warning("Unable to fetch tender details. The document might be in PDF format or require login.")
|
| 95 |
+
except Exception as e:
|
| 96 |
+
st.error(f"Error displaying active tenders: {str(e)}")
|
| 97 |
+
st.info("Try refreshing the page or check your internet connection.")
|
| 98 |
|
| 99 |
# PPRA Resources Tab
|
| 100 |
with tab3:
|
| 101 |
st.header("π PPRA Official Resources")
|
| 102 |
st.markdown("Below are official links to all relevant PPRA documentation and tender policies.")
|
| 103 |
resources = get_ppra_resources()
|
| 104 |
+
|
| 105 |
+
# Group resources by category for better organization
|
| 106 |
+
categories = {
|
| 107 |
+
"Main Links": ["Home", "Active Tenders"],
|
| 108 |
+
"Guidelines & Documents": ["Procurement Guidelines (PDF)", "PPRA Ordinance", "Rules"],
|
| 109 |
+
"Regulations": [k for k in resources.keys() if "Regulation" in k or "SRO" in k],
|
| 110 |
+
"Other Resources": ["Board Info", "Blacklisting & Debarment Regulations 2024"]
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
for category, resource_keys in categories.items():
|
| 114 |
+
st.subheader(category)
|
| 115 |
+
for key in resource_keys:
|
| 116 |
+
if key in resources:
|
| 117 |
+
st.markdown(f"- [{key}]({resources[key]})")
|