Spaces:
Build error
Build error
Update app.py
Browse files
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
|
| 9 |
|
| 10 |
-
# Set Hugging Face token from secrets
|
| 11 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
|
| 12 |
|
| 13 |
-
#
|
| 14 |
llm = HuggingFaceHub(
|
| 15 |
-
repo_id="
|
| 16 |
-
model_kwargs={"temperature": 0.7, "max_length": 1024}
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# Prompt
|
| 20 |
-
template = """
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 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("π
|
| 40 |
-
|
| 41 |
-
product2 = st.text_area("Enter Product 2 description")
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
st.subheader("π Analysis Report")
|
| 61 |
-
st.markdown(result)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 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 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|