AsthanM commited on
Commit
dc254e4
Β·
verified Β·
1 Parent(s): 5f56d69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -55
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import streamlit as st
2
- from langchain_community.llms import HuggingFaceHub
3
  import os
4
  from fpdf import FPDF
5
  from io import BytesIO
6
  import textwrap
7
  import re
8
 
9
- # Set page layout and dark theme styling
10
  st.set_page_config(page_title="Pro Competitive Analysis", layout="centered")
11
  st.markdown("""
12
  <style>
@@ -36,74 +36,63 @@ button:hover {
36
  </style>
37
  """, unsafe_allow_html=True)
38
 
39
- # App header
40
  st.title("πŸ’Ό Competitive Analysis Pro")
41
- st.markdown("Generate expert-level comparisons between two products or services, with SWOT, insights, and business recommendations.")
42
 
43
- # Set Hugging Face API key (securely via Secrets)
44
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
45
 
46
- # LLM Setup
47
- llm = HuggingFaceHub(
48
- repo_id="tiiuae/falcon-7b-instruct",
 
49
  model_kwargs={"temperature": 0.7, "max_new_tokens": 1024}
50
  )
51
 
52
- # Input Fields
53
  product1 = st.text_area("🧩 Product/Service 1", height=200, placeholder="e.g., iPhone 13")
54
  product2 = st.text_area("🧩 Product/Service 2", height=200, placeholder="e.g., iPhone 14")
55
 
56
- # Button logic
57
  if st.button("πŸ” Run Competitive Analysis", use_container_width=True):
58
  if not product1 or not product2:
59
  st.warning("Please enter both product descriptions.")
60
  else:
61
- # LLM prompt (not printed)
62
- system_instruction = """
63
- You are a professional market analyst trained to evaluate two competing products or services.
64
-
65
- Write an in-depth comparative analysis in **markdown format**, covering:
66
-
67
- 1. Feature-by-Feature Comparison:
68
-
69
- - For both Product 1 and Product 2 separately
70
-
71
- - Hardware specifications
72
- - Design and display
73
- - Performance (CPU, GPU, etc.)
74
- - Battery life and charging
75
- - Software experience
76
- - Ecosystem/integration
77
- - Customer support
78
- - Price and value proposition
79
-
80
- 2. SWOT Analysis:
81
- - For both Product 1 and Product 2 separately
82
-
83
- 3. Business Use Cases & Recommendations:
84
- - Target user base (consumer, enterprise, creators, etc.)
85
- - Best use cases
86
- - Strategic positioning
87
-
88
- 4. Key Differentiators and Summary:
89
- - Highlight which product is better for which audience
90
- - Summarize standout features
91
-
92
- Dont write these system_instructions while printing
93
- """
94
- user_input = f"\n\nProduct 1:\n{product1}\n\nProduct 2:\n{product2}"
95
- final_input = system_instruction.strip() + user_input
96
-
97
- with st.spinner("🧠 Generating detailed analysis..."):
98
- result = llm(final_input)
99
-
100
  st.markdown("### πŸ“Š Expert Comparison")
101
  st.markdown(result)
102
 
103
- # Clean markdown & sanitize output
104
  clean_text = result.replace("**", "").replace("*", "").replace("`", "").replace("#", "").replace("β€’", "-")
105
-
106
- # PDF Generation (safe against long unbreakable lines)
107
  pdf = FPDF()
108
  pdf.add_page()
109
  pdf.set_font("Arial", size=12)
@@ -123,14 +112,14 @@ Dont write these system_instructions while printing
123
  wrapped_lines = safe_line_split(line)
124
  for wl in wrapped_lines:
125
  pdf.multi_cell(0, 10, wl)
126
- except Exception as e:
127
- print(f"⚠️ Skipped line due to error: {line[:50]}...")
128
 
129
  pdf_output = BytesIO()
130
  pdf.output(pdf_output)
131
  pdf_output.seek(0)
132
 
133
- # Streamlit download button
134
  st.download_button(
135
  label="πŸ“₯ Download Report as PDF",
136
  data=pdf_output,
 
1
  import streamlit as st
2
+ from langchain_community.chat_models import ChatHuggingFace
3
  import os
4
  from fpdf import FPDF
5
  from io import BytesIO
6
  import textwrap
7
  import re
8
 
9
+ # Page config and custom dark theme
10
  st.set_page_config(page_title="Pro Competitive Analysis", layout="centered")
11
  st.markdown("""
12
  <style>
 
36
  </style>
37
  """, unsafe_allow_html=True)
38
 
39
+ # Header
40
  st.title("πŸ’Ό Competitive Analysis Pro")
41
+ st.markdown("Get a detailed, markdown-formatted expert comparison of two products or services. Including SWOT, features, use cases & more.")
42
 
43
+ # Set API token
44
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
45
 
46
+ # Load chat model (LLaMA 2)
47
+ llm = ChatHuggingFace(
48
+ repo_id="meta-llama/Llama-2-13b-chat-hf",
49
+ task="chat-completion",
50
  model_kwargs={"temperature": 0.7, "max_new_tokens": 1024}
51
  )
52
 
53
+ # User input
54
  product1 = st.text_area("🧩 Product/Service 1", height=200, placeholder="e.g., iPhone 13")
55
  product2 = st.text_area("🧩 Product/Service 2", height=200, placeholder="e.g., iPhone 14")
56
 
57
+ # Generate button
58
  if st.button("πŸ” Run Competitive Analysis", use_container_width=True):
59
  if not product1 or not product2:
60
  st.warning("Please enter both product descriptions.")
61
  else:
62
+ with st.spinner("🧠 Analyzing with LLaMA 2..."):
63
+
64
+ # Construct system & user prompt (chat style)
65
+ chat_input = [
66
+ {
67
+ "role": "system",
68
+ "content": "You are a professional market analyst. Only return markdown-formatted analysis β€” do not include system instructions."
69
+ },
70
+ {
71
+ "role": "user",
72
+ "content": f"""Compare the following two products/services:
73
+
74
+ Product 1:
75
+ {product1}
76
+
77
+ Product 2:
78
+ {product2}
79
+
80
+ Return a detailed analysis in markdown format that includes:
81
+ - Feature-by-feature comparison
82
+ - SWOT analysis for each product
83
+ - Business use cases and recommendations
84
+ - Key differentiators and which product suits which audience"""
85
+ }
86
+ ]
87
+
88
+ result = llm(chat_input)
89
+
90
+ # Display result
 
 
 
 
 
 
 
 
 
 
91
  st.markdown("### πŸ“Š Expert Comparison")
92
  st.markdown(result)
93
 
94
+ # Clean & wrap output for PDF
95
  clean_text = result.replace("**", "").replace("*", "").replace("`", "").replace("#", "").replace("β€’", "-")
 
 
96
  pdf = FPDF()
97
  pdf.add_page()
98
  pdf.set_font("Arial", size=12)
 
112
  wrapped_lines = safe_line_split(line)
113
  for wl in wrapped_lines:
114
  pdf.multi_cell(0, 10, wl)
115
+ except Exception:
116
+ continue # Skip bad lines
117
 
118
  pdf_output = BytesIO()
119
  pdf.output(pdf_output)
120
  pdf_output.seek(0)
121
 
122
+ # Download button
123
  st.download_button(
124
  label="πŸ“₯ Download Report as PDF",
125
  data=pdf_output,