Update app.py
Browse files
app.py
CHANGED
|
@@ -7,29 +7,15 @@ import matplotlib.pyplot as plt
|
|
| 7 |
from fpdf import FPDF
|
| 8 |
from io import BytesIO
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
st.set_page_config(page_title="Competitive Analysis Tool", layout="wide")
|
| 12 |
-
st.title("π Competitive Analysis Tool")
|
| 13 |
-
st.markdown("""
|
| 14 |
-
Welcome to your AI-powered business analyst.
|
| 15 |
-
Compare two products or services to receive:
|
| 16 |
-
- π§© Feature-by-feature comparison
|
| 17 |
-
- π‘ SWOT analysis
|
| 18 |
-
- π Key differentiators
|
| 19 |
-
- π₯ Downloadable PDF Report
|
| 20 |
-
""")
|
| 21 |
-
|
| 22 |
-
st.markdown("---")
|
| 23 |
-
|
| 24 |
-
# π Set API token
|
| 25 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
|
| 26 |
|
| 27 |
-
#
|
| 28 |
llm = HuggingFaceHub(
|
| 29 |
-
repo_id="tiiuae/falcon-7b-instruct",
|
| 30 |
-
model_kwargs={"temperature": 0.7, "max_new_tokens": 1024}
|
| 31 |
)
|
| 32 |
|
|
|
|
| 33 |
template = """
|
| 34 |
Compare the following two products or services:
|
| 35 |
Product 1:
|
|
@@ -42,32 +28,29 @@ Instructions:
|
|
| 42 |
3. Summarize key differentiators between them.
|
| 43 |
"""
|
| 44 |
|
| 45 |
-
prompt = PromptTemplate(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
comparison_chain = LLMChain(llm=llm, prompt=prompt)
|
| 47 |
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
product1 = st.text_area("π§© Product/Service 1", height=200, placeholder="Describe Product 1...")
|
| 52 |
-
with col2:
|
| 53 |
-
product2 = st.text_area("π§© Product/Service 2", height=200, placeholder="Describe Product 2...")
|
| 54 |
|
| 55 |
-
st.
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
if st.button("βοΈ Compare Now", use_container_width=True):
|
| 59 |
if not product1 or not product2:
|
| 60 |
st.warning("Please enter descriptions for both products.")
|
| 61 |
else:
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# π§ Display Results
|
| 66 |
-
st.subheader("π AI-Generated Competitive Analysis")
|
| 67 |
-
st.markdown(f"""<div style='background-color:#f9f9f9;padding:20px;border-radius:10px;'>{result}</div>""", unsafe_allow_html=True)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
st.markdown("### π’ Word Count Breakdown")
|
| 71 |
p1_len = len(product1.split())
|
| 72 |
p2_len = len(product2.split())
|
| 73 |
fig, ax = plt.subplots()
|
|
@@ -76,11 +59,12 @@ if st.button("βοΈ Compare Now", use_container_width=True):
|
|
| 76 |
ax.set_title("Word Count Comparison")
|
| 77 |
st.pyplot(fig)
|
| 78 |
|
| 79 |
-
#
|
| 80 |
pdf = FPDF()
|
| 81 |
pdf.add_page()
|
| 82 |
pdf.set_font("Arial", size=12)
|
| 83 |
pdf.multi_cell(0, 10, txt="Competitive Analysis Report\n\n" + result)
|
|
|
|
| 84 |
pdf_bytes = pdf.output(dest='S').encode('latin1')
|
| 85 |
pdf_output = BytesIO(pdf_bytes)
|
| 86 |
|
|
@@ -88,6 +72,5 @@ if st.button("βοΈ Compare Now", use_container_width=True):
|
|
| 88 |
label="π₯ Download PDF Report",
|
| 89 |
data=pdf_output,
|
| 90 |
file_name="competitive_analysis_report.pdf",
|
| 91 |
-
mime="application/pdf"
|
| 92 |
-
use_container_width=True
|
| 93 |
)
|
|
|
|
| 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:
|
|
|
|
| 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()
|
|
|
|
| 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 |
|
|
|
|
| 72 |
label="π₯ Download PDF Report",
|
| 73 |
data=pdf_output,
|
| 74 |
file_name="competitive_analysis_report.pdf",
|
| 75 |
+
mime="application/pdf"
|
|
|
|
| 76 |
)
|