Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,7 @@ from crew_initializer import initialize_crew
|
|
3 |
from utils.pdf_generator import generate_pdf
|
4 |
import json
|
5 |
import logging
|
|
|
6 |
|
7 |
# Configure logging
|
8 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
@@ -18,37 +19,98 @@ class CustomJSONEncoder(json.JSONEncoder):
|
|
18 |
except TypeError:
|
19 |
return str(obj) # Fallback for unsupported types
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def main():
|
22 |
"""
|
23 |
Main entry point for the Streamlit application.
|
24 |
Handles user input, executes tasks, and displays results.
|
25 |
"""
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
st.sidebar.header("Provide Company Details")
|
29 |
-
company_name = st.sidebar.text_input("Enter the Company Name:"
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
if __name__ == "__main__":
|
54 |
main()
|
|
|
3 |
from utils.pdf_generator import generate_pdf
|
4 |
import json
|
5 |
import logging
|
6 |
+
import re
|
7 |
|
8 |
# Configure logging
|
9 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
19 |
except TypeError:
|
20 |
return str(obj) # Fallback for unsupported types
|
21 |
|
22 |
+
# Hardcoded credentials
|
23 |
+
USER_CREDENTIALS = {"username": "admin", "password": "P@ssword123"}
|
24 |
+
|
25 |
+
def authenticate_user():
|
26 |
+
"""Authenticate the user with a username and password"""
|
27 |
+
st.sidebar.header("Login")
|
28 |
+
username = st.sidebar.text_input("Username")
|
29 |
+
password = st.sidebar.text_input("Password", type="password")
|
30 |
+
|
31 |
+
if st.sidebar.button("Login"):
|
32 |
+
if username == USER_CREDENTIALS["username"] and password == USER_CREDENTIALS["password"]:
|
33 |
+
st.session_state.authenticated = True
|
34 |
+
st.sidebar.success("Login successful!")
|
35 |
+
st.experimental_rerun() # Rerun to clear login form and show the app
|
36 |
+
else:
|
37 |
+
st.sidebar.error("Invalid username or password.")
|
38 |
+
|
39 |
def main():
|
40 |
"""
|
41 |
Main entry point for the Streamlit application.
|
42 |
Handles user input, executes tasks, and displays results.
|
43 |
"""
|
44 |
+
# Ensure authentication state is properly initialized
|
45 |
+
if 'authenticated' not in st.session_state:
|
46 |
+
st.session_state.authenticated = False
|
47 |
+
|
48 |
+
# Authenticate user if not authenticated
|
49 |
+
if not st.session_state.authenticated:
|
50 |
+
authenticate_user()
|
51 |
+
return # Exit early if not authenticated
|
52 |
+
|
53 |
+
# Once authenticated, proceed with the main application
|
54 |
+
st.title("**Company Researcher Tool**")
|
55 |
|
56 |
st.sidebar.header("Provide Company Details")
|
57 |
+
company_name = st.sidebar.text_input("Enter the Company Name:")
|
58 |
+
|
59 |
+
# Show the "Run Analysis" button if the user is authenticated
|
60 |
+
if st.session_state.authenticated:
|
61 |
+
# Show the button whether or not the company name is entered
|
62 |
+
run_analysis_button = st.sidebar.button("Run Analysis")
|
63 |
+
|
64 |
+
# Only run the analysis when the button is clicked
|
65 |
+
if run_analysis_button:
|
66 |
+
st.markdown(f"### **Running analysis for:** {company_name}", unsafe_allow_html=True)
|
67 |
+
|
68 |
+
with st.spinner("Executing tasks, please wait..."):
|
69 |
+
try:
|
70 |
+
crew = initialize_crew()
|
71 |
+
result = crew.kickoff(inputs={"company": company_name})
|
72 |
+
result_serialized = json.loads(json.dumps(result, cls=CustomJSONEncoder))
|
73 |
+
|
74 |
+
# Extracting the raw summary report
|
75 |
+
summary_report = result_serialized.get('tasks_output', [{}])[0].get('raw', '')
|
76 |
+
|
77 |
+
# Formatting the raw content into structured report
|
78 |
+
structured_report = format_report(summary_report)
|
79 |
+
|
80 |
+
st.markdown(f"### **Summary Report**", unsafe_allow_html=True)
|
81 |
+
st.markdown(structured_report, unsafe_allow_html=True)
|
82 |
+
|
83 |
+
pdf_buffer = generate_pdf(result_serialized)
|
84 |
+
st.download_button(
|
85 |
+
label="📄 Download Report Of Token Used (PDF)",
|
86 |
+
data=pdf_buffer,
|
87 |
+
file_name=f"{company_name}_token_report.pdf",
|
88 |
+
mime="application/pdf"
|
89 |
+
)
|
90 |
+
except Exception as e:
|
91 |
+
logging.error(f"Error during analysis: {str(e)}")
|
92 |
+
st.error(f"An error occurred: {str(e)}")
|
93 |
+
else:
|
94 |
+
st.info("Please enter a company name to run the analysis.")
|
95 |
+
|
96 |
+
def format_report(raw_text):
|
97 |
+
"""
|
98 |
+
Takes raw text (markdown/structured content) and formats it to
|
99 |
+
create a more readable, structured report with bold headers.
|
100 |
+
"""
|
101 |
+
# This pattern looks for headings with a number followed by bold text and a colon (e.g. 1. **Heading**:)
|
102 |
+
heading_pattern = r'(\d+\.)\s?(\*\*[^*]+\*\*):'
|
103 |
+
|
104 |
+
# Replace headings with Markdown syntax (e.g., ## **Heading**:)
|
105 |
+
formatted_report = re.sub(heading_pattern, r'## \2:', raw_text)
|
106 |
+
|
107 |
+
# Replace **bold text** with proper markdown formatting
|
108 |
+
formatted_report = re.sub(r'\*\*(.*?)\*\*', r'**\1**', formatted_report)
|
109 |
+
|
110 |
+
# Convert newlines to <br> for better readability in Streamlit
|
111 |
+
formatted_report = formatted_report.replace("\n", "<br>")
|
112 |
+
|
113 |
+
return formatted_report
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
main()
|