GIRI45 commited on
Commit
7aef27a
Β·
verified Β·
1 Parent(s): d54ded6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -57
app.py CHANGED
@@ -1,33 +1,30 @@
1
  import os
2
  import streamlit as st
 
3
  from langchain.prompts import PromptTemplate
4
  from langchain.chains import LLMChain
5
- from langchain_community.llms import HuggingFaceHub
6
  import matplotlib.pyplot as plt
7
- from io import BytesIO
8
- from reportlab.pdfgen import canvas
9
- from reportlab.lib.pagesizes import letter
10
 
11
- # Load Hugging Face token
12
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
13
 
14
- # Initialize LLM
15
  llm = HuggingFaceHub(
16
  repo_id="google/flan-t5-xl",
17
- model_kwargs={"temperature": 0.5, "max_length": 1024}
18
  )
19
 
20
  # Prompt Template
21
- template = """
22
- Compare the following two products or services:
23
  Product 1: {product1}
24
  Product 2: {product2}
25
 
26
- 1. Feature-by-feature comparison.
27
- 2. SWOT analysis for each.
28
- 3. A comparative summary highlighting key differentiators.
29
-
30
- Respond in structured format with headings.
31
  """
32
 
33
  prompt = PromptTemplate(
@@ -35,61 +32,56 @@ prompt = PromptTemplate(
35
  template=template,
36
  )
37
 
 
38
  comparison_chain = LLMChain(llm=llm, prompt=prompt)
39
 
40
  # Streamlit UI
41
- st.title("πŸ” Competitive Analysis Tool (Hugging Face Version)")
42
-
43
- product1 = st.text_area("Enter details for Product/Service 1")
44
- product2 = st.text_area("Enter details for Product/Service 2")
 
 
 
 
 
 
 
 
45
 
46
  if st.button("Compare"):
47
- with st.spinner("Generating analysis..."):
48
- try:
 
 
49
  result = comparison_chain.run(product1=product1, product2=product2)
50
- st.subheader("πŸ“Š Analysis Result")
 
 
51
  st.markdown(result)
52
 
53
- # Optional: Simple chart visualization (Dummy feature scores)
54
- features = ["Usability", "Performance", "Support", "Integration"]
55
- p1_scores = [8, 7, 6, 9]
56
- p2_scores = [7, 8, 8, 7]
 
57
 
58
  fig, ax = plt.subplots()
59
- bar_width = 0.35
60
- index = range(len(features))
61
-
62
- ax.bar(index, p1_scores, bar_width, label="Product 1")
63
- ax.bar([i + bar_width for i in index], p2_scores, bar_width, label="Product 2")
64
-
65
- ax.set_xlabel('Features')
66
- ax.set_ylabel('Scores')
67
- ax.set_title('Feature Comparison')
68
- ax.set_xticks([i + bar_width / 2 for i in index])
69
  ax.set_xticklabels(features)
70
  ax.legend()
71
-
72
  st.pyplot(fig)
73
 
74
  # PDF Download
75
- def create_pdf(text):
76
- buffer = BytesIO()
77
- c = canvas.Canvas(buffer, pagesize=letter)
78
- width, height = letter
79
- y = height - 40
80
-
81
- for line in text.split('\n'):
82
- c.drawString(30, y, line)
83
- y -= 15
84
- if y < 50:
85
- c.showPage()
86
- y = height - 40
87
- c.save()
88
- buffer.seek(0)
89
- return buffer
90
-
91
- pdf_data = create_pdf(result)
92
- st.download_button("πŸ“„ Download PDF Report", data=pdf_data, file_name="analysis_report.pdf")
93
-
94
- except Exception as e:
95
- st.error(f"Something went wrong: {e}")
 
1
  import os
2
  import streamlit as st
3
+ from langchain_community.llms import HuggingFaceHub
4
  from langchain.prompts import PromptTemplate
5
  from langchain.chains import LLMChain
 
6
  import matplotlib.pyplot as plt
7
+ from fpdf import FPDF
8
+ import tempfile
 
9
 
10
+ # Set Hugging Face token from secrets
11
  os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
12
 
13
+ # Load LLM from Hugging Face
14
  llm = HuggingFaceHub(
15
  repo_id="google/flan-t5-xl",
16
+ model_kwargs={"temperature": 0.7, "max_length": 1024}
17
  )
18
 
19
  # Prompt Template
20
+ template = """Compare the following two products/services:
 
21
  Product 1: {product1}
22
  Product 2: {product2}
23
 
24
+ Provide:
25
+ 1. A feature-by-feature comparison (up to 5 features).
26
+ 2. SWOT analysis for both.
27
+ 3. A comparative summary highlighting key differences and similarities.
 
28
  """
29
 
30
  prompt = PromptTemplate(
 
32
  template=template,
33
  )
34
 
35
+ # LLM Chain
36
  comparison_chain = LLMChain(llm=llm, prompt=prompt)
37
 
38
  # Streamlit UI
39
+ st.title("πŸ” AI Competitive Analysis Tool")
40
+ product1 = st.text_area("Enter Product 1 description")
41
+ product2 = st.text_area("Enter Product 2 description")
42
+
43
+ def generate_pdf(text, filename):
44
+ pdf = FPDF()
45
+ pdf.add_page()
46
+ pdf.set_auto_page_break(auto=True, margin=15)
47
+ pdf.set_font("Arial", size=12)
48
+ for line in text.split('\n'):
49
+ pdf.multi_cell(0, 10, line)
50
+ pdf.output(filename)
51
 
52
  if st.button("Compare"):
53
+ if not product1 or not product2:
54
+ st.warning("Please enter both products.")
55
+ else:
56
+ with st.spinner("Analyzing..."):
57
  result = comparison_chain.run(product1=product1, product2=product2)
58
+ st.success("Comparison complete!")
59
+
60
+ st.subheader("πŸ“‹ Analysis Report")
61
  st.markdown(result)
62
 
63
+ # Optional Chart (dummy data, just for visual comparison)
64
+ st.subheader("πŸ“Š Feature Comparison Chart")
65
+ features = ["Ease of Use", "Performance", "Cost", "Support", "Scalability"]
66
+ values1 = [3, 4, 2, 3, 4] # Dummy data
67
+ values2 = [4, 3, 3, 4, 3] # Dummy data
68
 
69
  fig, ax = plt.subplots()
70
+ x = range(len(features))
71
+ ax.bar(x, values1, width=0.4, label="Product 1", align='center')
72
+ ax.bar([p + 0.4 for p in x], values2, width=0.4, label="Product 2", align='center')
73
+ ax.set_xticks([p + 0.2 for p in x])
 
 
 
 
 
 
74
  ax.set_xticklabels(features)
75
  ax.legend()
 
76
  st.pyplot(fig)
77
 
78
  # PDF Download
79
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
80
+ generate_pdf(result, tmp_file.name)
81
+ with open(tmp_file.name, "rb") as f:
82
+ st.download_button(
83
+ label="πŸ“„ Download PDF Report",
84
+ data=f,
85
+ file_name="comparison_report.pdf",
86
+ mime="application/pdf"
87
+ )