AsthanM commited on
Commit
62f71c8
·
verified ·
1 Parent(s): 5d4e251

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -31
app.py CHANGED
@@ -1,39 +1,76 @@
1
  import streamlit as st
2
- import requests
 
 
3
  import os
 
 
 
4
 
5
- st.set_page_config(page_title="Competitive Product Analyser", layout="wide")
6
- st.title("🔍 Competitive Product Analyser")
7
- st.markdown("Compare two products or services using LLM-powered feature and SWOT analysis.")
8
 
9
- product1 = st.text_area("🧩 Product/Service 1 Description", height=150)
10
- product2 = st.text_area("🧩 Product/Service 2 Description", height=150)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  if st.button("Compare"):
13
  if not product1 or not product2:
14
- st.warning("Please enter both product descriptions.")
15
  else:
16
- with st.spinner("Generating analysis..."):
17
- prompt = f"""Compare the following two products/services:
18
-
19
- Product 1: {product1}
20
-
21
- Product 2: {product2}
22
-
23
- Provide:
24
- 1. A feature-by-feature comparison.
25
- 2. A SWOT analysis for each product/service.
26
- 3. A comparative summary highlighting key differentiators."""
27
-
28
- api_url = "https://api-inference.huggingface.co/models/google/flan-t5-large"
29
- headers = {"Authorization": f"Bearer {os.getenv('HF_API_TOKEN')}"}
30
-
31
- response = requests.post(api_url, headers=headers, json={"inputs": prompt})
32
-
33
- if response.status_code == 200:
34
- output = response.json()
35
- result = output[0]["generated_text"] if isinstance(output, list) else output.get("generated_text", "")
36
- st.subheader("🧠 Analysis")
37
- st.write(result)
38
- else:
39
- st.error(f"API Error: {response.status_code} — {response.text}")
 
 
 
 
 
1
  import streamlit as st
2
+ 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
+ )