AsthanM commited on
Commit
1c8fd1c
Β·
verified Β·
1 Parent(s): dd59a4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -35
app.py CHANGED
@@ -3,74 +3,100 @@ from langchain_community.llms import HuggingFaceHub
3
  from langchain.prompts import PromptTemplate
4
  from langchain.chains import LLMChain
5
  import os
6
- import matplotlib.pyplot as plt
7
  from fpdf import FPDF
8
  from io import BytesIO
9
 
10
- # Set Hugging Face API token from secrets
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
12
 
13
- # Choose a compatible model (text-generation)
14
  llm = HuggingFaceHub(
15
- repo_id="tiiuae/falcon-7b-instruct", model_kwargs={"temperature": 0.7, "max_new_tokens": 1024}
 
16
  )
17
 
18
  # Prompt template
19
  template = """
20
- Compare the following two products or services:
 
21
  Product 1:
22
  {product1}
 
23
  Product 2:
24
  {product2}
 
25
  Instructions:
26
- 1. Provide a feature-by-feature comparison.
27
- 2. Generate a SWOT (Strengths, Weaknesses, Opportunities, Threats) analysis for each.
28
- 3. Summarize key differentiators between them.
 
 
29
  """
30
 
31
- prompt = PromptTemplate(
32
- input_variables=["product1", "product2"],
33
- template=template,
34
- )
35
-
36
  comparison_chain = LLMChain(llm=llm, prompt=prompt)
37
 
38
- # Streamlit UI
39
- st.title("πŸ” Competitive Analysis Tool")
40
- st.write("Compare two products or services using LLM-powered insights.")
41
-
42
- product1 = st.text_area("Enter Product/Service 1 Description", height=200)
43
- product2 = st.text_area("Enter Product/Service 2 Description", height=200)
44
 
45
- if st.button("Compare"):
46
  if not product1 or not product2:
47
  st.warning("Please enter descriptions for both products.")
48
  else:
49
- result = comparison_chain.run(product1=product1, product2=product2)
50
- st.subheader("πŸ“Š Comparison Results")
51
- st.write(result)
52
 
53
- # Basic chart: Number of keywords in each description (just an example)
54
- p1_len = len(product1.split())
55
- p2_len = len(product2.split())
56
- fig, ax = plt.subplots()
57
- ax.bar(["Product 1", "Product 2"], [p1_len, p2_len], color=["skyblue", "salmon"])
58
- ax.set_ylabel("Word Count")
59
- ax.set_title("Word Count Comparison")
60
- st.pyplot(fig)
61
 
62
- # Create a downloadable PDF report
63
  pdf = FPDF()
64
  pdf.add_page()
65
  pdf.set_font("Arial", size=12)
66
  pdf.multi_cell(0, 10, txt="Competitive Analysis Report\n\n" + result)
67
- # Generate PDF content as string and convert to BytesIO
68
  pdf_bytes = pdf.output(dest='S').encode('latin1')
69
  pdf_output = BytesIO(pdf_bytes)
70
 
71
  st.download_button(
72
- label="πŸ“₯ Download PDF Report",
73
  data=pdf_output,
74
  file_name="competitive_analysis_report.pdf",
75
- mime="application/pdf"
 
76
  )
 
3
  from langchain.prompts import PromptTemplate
4
  from langchain.chains import LLMChain
5
  import os
 
6
  from fpdf import FPDF
7
  from io import BytesIO
8
 
9
+ # Page config
10
+ st.set_page_config(page_title="Pro Competitive Analysis Tool", layout="centered")
11
+
12
+ # Stylish monochrome header
13
+ st.markdown("""
14
+ <style>
15
+ body {
16
+ background-color: #111;
17
+ color: #e0e0e0;
18
+ font-family: 'Segoe UI', sans-serif;
19
+ }
20
+ h1, h2, h3 {
21
+ color: #ffffff;
22
+ }
23
+ textarea, input, button, .stTextInput>div>div>input {
24
+ background-color: #1c1c1c !important;
25
+ color: #f1f1f1 !important;
26
+ border: 1px solid #444 !important;
27
+ }
28
+ button:hover {
29
+ background-color: #444 !important;
30
+ }
31
+ .stDownloadButton>button {
32
+ background-color: #222 !important;
33
+ color: white;
34
+ }
35
+ .stDownloadButton>button:hover {
36
+ background-color: #444 !important;
37
+ }
38
+ </style>
39
+ """, unsafe_allow_html=True)
40
+
41
+ st.title("πŸ’Ό Competitive Analysis Pro")
42
+ st.markdown("Gain deep market insight with feature breakdowns, SWOT, and key differentiators.")
43
+
44
+ # Set API key from Hugging Face Secrets
45
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
46
 
47
+ # Load model
48
  llm = HuggingFaceHub(
49
+ repo_id="tiiuae/falcon-7b-instruct",
50
+ model_kwargs={"temperature": 0.7, "max_new_tokens": 1024}
51
  )
52
 
53
  # Prompt template
54
  template = """
55
+ You are a professional market analyst. Analyze the two following products/services.
56
+
57
  Product 1:
58
  {product1}
59
+
60
  Product 2:
61
  {product2}
62
+
63
  Instructions:
64
+ 1. Provide a detailed feature-by-feature comparison (performance, pricing, usability, support, integrations, etc.).
65
+ 2. Conduct a comprehensive SWOT (Strengths, Weaknesses, Opportunities, Threats) analysis for each.
66
+ 3. Offer business insights, use cases, and suggestions for each.
67
+ 4. Summarize the key differentiators and recommend which is better for different types of users or companies.
68
+ Use professional tone and structured markdown formatting (with bold headings and bullet points).
69
  """
70
 
71
+ prompt = PromptTemplate(input_variables=["product1", "product2"], template=template)
 
 
 
 
72
  comparison_chain = LLMChain(llm=llm, prompt=prompt)
73
 
74
+ # Input UI
75
+ product1 = st.text_area("🧩 Product/Service 1", height=200, placeholder="e.g., Salesforce CRM")
76
+ product2 = st.text_area("🧩 Product/Service 2", height=200, placeholder="e.g., Zoho CRM")
 
 
 
77
 
78
+ if st.button("πŸ” Run Competitive Analysis", use_container_width=True):
79
  if not product1 or not product2:
80
  st.warning("Please enter descriptions for both products.")
81
  else:
82
+ with st.spinner("Analyzing with LLM..."):
83
+ result = comparison_chain.run(product1=product1, product2=product2)
 
84
 
85
+ st.markdown("### πŸ“Š Analysis Report")
86
+ st.markdown(result)
 
 
 
 
 
 
87
 
88
+ # PDF Report
89
  pdf = FPDF()
90
  pdf.add_page()
91
  pdf.set_font("Arial", size=12)
92
  pdf.multi_cell(0, 10, txt="Competitive Analysis Report\n\n" + result)
 
93
  pdf_bytes = pdf.output(dest='S').encode('latin1')
94
  pdf_output = BytesIO(pdf_bytes)
95
 
96
  st.download_button(
97
+ label="πŸ“₯ Download Full Report as PDF",
98
  data=pdf_output,
99
  file_name="competitive_analysis_report.pdf",
100
+ mime="application/pdf",
101
+ use_container_width=True
102
  )