GIRI45 commited on
Commit
c64a253
Β·
verified Β·
1 Parent(s): 788a226

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -56
app.py CHANGED
@@ -1,30 +1,34 @@
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,56 +36,44 @@ 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
- )
 
 
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
+
22
+ Product 1:
23
+ {product1}
24
 
25
+ Product 2:
26
+ {product2}
27
+
28
+ Instructions:
29
+ 1. Provide a feature-by-feature comparison.
30
+ 2. Generate a SWOT (Strengths, Weaknesses, Opportunities, Threats) analysis for each.
31
+ 3. Summarize key differentiators between them.
32
  """
33
 
34
  prompt = PromptTemplate(
 
36
  template=template,
37
  )
38
 
 
39
  comparison_chain = LLMChain(llm=llm, prompt=prompt)
40
 
41
  # Streamlit UI
42
+ st.title("πŸ” Competitive Analysis Tool")
43
+ st.write("Compare two products or services using LLM-powered insights.")
 
44
 
45
+ product1 = st.text_area("Enter Product/Service 1 Description", height=200)
46
+ product2 = st.text_area("Enter Product/Service 2 Description", height=200)
 
 
 
 
 
 
47
 
48
  if st.button("Compare"):
49
  if not product1 or not product2:
50
+ st.warning("Please enter descriptions for both products.")
51
  else:
52
+ result = comparison_chain.run(product1=product1, product2=product2)
53
+ st.subheader("πŸ“Š Comparison Results")
54
+ st.write(result)
 
 
 
55
 
56
+ # Basic chart: Number of keywords in each description (just an example)
57
+ p1_len = len(product1.split())
58
+ p2_len = len(product2.split())
59
+ fig, ax = plt.subplots()
60
+ ax.bar(["Product 1", "Product 2"], [p1_len, p2_len], color=["skyblue", "salmon"])
61
+ ax.set_ylabel("Word Count")
62
+ ax.set_title("Word Count Comparison")
63
+ st.pyplot(fig)
64
 
65
+ # Create a downloadable PDF report
66
+ pdf = FPDF()
67
+ pdf.add_page()
68
+ pdf.set_font("Arial", size=12)
69
+ pdf.multi_cell(0, 10, txt="Competitive Analysis Report\n\n" + result)
70
+ pdf_output = BytesIO()
71
+ pdf.output(pdf_output)
72
+ pdf_output.seek(0)
73
 
74
+ st.download_button(
75
+ label="πŸ“₯ Download PDF Report",
76
+ data=pdf_output,
77
+ file_name="competitive_analysis_report.pdf",
78
+ mime="application/pdf"
79
+ )