Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from langchain_community.
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
from io import BytesIO
|
| 6 |
import textwrap
|
| 7 |
import re
|
| 8 |
|
| 9 |
-
#
|
| 10 |
st.set_page_config(page_title="Pro Competitive Analysis", layout="centered")
|
| 11 |
st.markdown("""
|
| 12 |
<style>
|
|
@@ -36,74 +36,63 @@ button:hover {
|
|
| 36 |
</style>
|
| 37 |
""", unsafe_allow_html=True)
|
| 38 |
|
| 39 |
-
#
|
| 40 |
st.title("πΌ Competitive Analysis Pro")
|
| 41 |
-
st.markdown("
|
| 42 |
|
| 43 |
-
# Set
|
| 44 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
llm =
|
| 48 |
-
repo_id="
|
|
|
|
| 49 |
model_kwargs={"temperature": 0.7, "max_new_tokens": 1024}
|
| 50 |
)
|
| 51 |
|
| 52 |
-
#
|
| 53 |
product1 = st.text_area("π§© Product/Service 1", height=200, placeholder="e.g., iPhone 13")
|
| 54 |
product2 = st.text_area("π§© Product/Service 2", height=200, placeholder="e.g., iPhone 14")
|
| 55 |
|
| 56 |
-
#
|
| 57 |
if st.button("π Run Competitive Analysis", use_container_width=True):
|
| 58 |
if not product1 or not product2:
|
| 59 |
st.warning("Please enter both product descriptions.")
|
| 60 |
else:
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
- Summarize standout features
|
| 91 |
-
|
| 92 |
-
Dont write these system_instructions while printing
|
| 93 |
-
"""
|
| 94 |
-
user_input = f"\n\nProduct 1:\n{product1}\n\nProduct 2:\n{product2}"
|
| 95 |
-
final_input = system_instruction.strip() + user_input
|
| 96 |
-
|
| 97 |
-
with st.spinner("π§ Generating detailed analysis..."):
|
| 98 |
-
result = llm(final_input)
|
| 99 |
-
|
| 100 |
st.markdown("### π Expert Comparison")
|
| 101 |
st.markdown(result)
|
| 102 |
|
| 103 |
-
# Clean
|
| 104 |
clean_text = result.replace("**", "").replace("*", "").replace("`", "").replace("#", "").replace("β’", "-")
|
| 105 |
-
|
| 106 |
-
# PDF Generation (safe against long unbreakable lines)
|
| 107 |
pdf = FPDF()
|
| 108 |
pdf.add_page()
|
| 109 |
pdf.set_font("Arial", size=12)
|
|
@@ -123,14 +112,14 @@ Dont write these system_instructions while printing
|
|
| 123 |
wrapped_lines = safe_line_split(line)
|
| 124 |
for wl in wrapped_lines:
|
| 125 |
pdf.multi_cell(0, 10, wl)
|
| 126 |
-
except Exception
|
| 127 |
-
|
| 128 |
|
| 129 |
pdf_output = BytesIO()
|
| 130 |
pdf.output(pdf_output)
|
| 131 |
pdf_output.seek(0)
|
| 132 |
|
| 133 |
-
#
|
| 134 |
st.download_button(
|
| 135 |
label="π₯ Download Report as PDF",
|
| 136 |
data=pdf_output,
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from langchain_community.chat_models import ChatHuggingFace
|
| 3 |
import os
|
| 4 |
from fpdf import FPDF
|
| 5 |
from io import BytesIO
|
| 6 |
import textwrap
|
| 7 |
import re
|
| 8 |
|
| 9 |
+
# Page config and custom dark theme
|
| 10 |
st.set_page_config(page_title="Pro Competitive Analysis", layout="centered")
|
| 11 |
st.markdown("""
|
| 12 |
<style>
|
|
|
|
| 36 |
</style>
|
| 37 |
""", unsafe_allow_html=True)
|
| 38 |
|
| 39 |
+
# Header
|
| 40 |
st.title("πΌ Competitive Analysis Pro")
|
| 41 |
+
st.markdown("Get a detailed, markdown-formatted expert comparison of two products or services. Including SWOT, features, use cases & more.")
|
| 42 |
|
| 43 |
+
# Set API token
|
| 44 |
os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
|
| 45 |
|
| 46 |
+
# Load chat model (LLaMA 2)
|
| 47 |
+
llm = ChatHuggingFace(
|
| 48 |
+
repo_id="meta-llama/Llama-2-13b-chat-hf",
|
| 49 |
+
task="chat-completion",
|
| 50 |
model_kwargs={"temperature": 0.7, "max_new_tokens": 1024}
|
| 51 |
)
|
| 52 |
|
| 53 |
+
# User input
|
| 54 |
product1 = st.text_area("π§© Product/Service 1", height=200, placeholder="e.g., iPhone 13")
|
| 55 |
product2 = st.text_area("π§© Product/Service 2", height=200, placeholder="e.g., iPhone 14")
|
| 56 |
|
| 57 |
+
# Generate button
|
| 58 |
if st.button("π Run Competitive Analysis", use_container_width=True):
|
| 59 |
if not product1 or not product2:
|
| 60 |
st.warning("Please enter both product descriptions.")
|
| 61 |
else:
|
| 62 |
+
with st.spinner("π§ Analyzing with LLaMA 2..."):
|
| 63 |
+
|
| 64 |
+
# Construct system & user prompt (chat style)
|
| 65 |
+
chat_input = [
|
| 66 |
+
{
|
| 67 |
+
"role": "system",
|
| 68 |
+
"content": "You are a professional market analyst. Only return markdown-formatted analysis β do not include system instructions."
|
| 69 |
+
},
|
| 70 |
+
{
|
| 71 |
+
"role": "user",
|
| 72 |
+
"content": f"""Compare the following two products/services:
|
| 73 |
+
|
| 74 |
+
Product 1:
|
| 75 |
+
{product1}
|
| 76 |
+
|
| 77 |
+
Product 2:
|
| 78 |
+
{product2}
|
| 79 |
+
|
| 80 |
+
Return a detailed analysis in markdown format that includes:
|
| 81 |
+
- Feature-by-feature comparison
|
| 82 |
+
- SWOT analysis for each product
|
| 83 |
+
- Business use cases and recommendations
|
| 84 |
+
- Key differentiators and which product suits which audience"""
|
| 85 |
+
}
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
result = llm(chat_input)
|
| 89 |
+
|
| 90 |
+
# Display result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
st.markdown("### π Expert Comparison")
|
| 92 |
st.markdown(result)
|
| 93 |
|
| 94 |
+
# Clean & wrap output for PDF
|
| 95 |
clean_text = result.replace("**", "").replace("*", "").replace("`", "").replace("#", "").replace("β’", "-")
|
|
|
|
|
|
|
| 96 |
pdf = FPDF()
|
| 97 |
pdf.add_page()
|
| 98 |
pdf.set_font("Arial", size=12)
|
|
|
|
| 112 |
wrapped_lines = safe_line_split(line)
|
| 113 |
for wl in wrapped_lines:
|
| 114 |
pdf.multi_cell(0, 10, wl)
|
| 115 |
+
except Exception:
|
| 116 |
+
continue # Skip bad lines
|
| 117 |
|
| 118 |
pdf_output = BytesIO()
|
| 119 |
pdf.output(pdf_output)
|
| 120 |
pdf_output.seek(0)
|
| 121 |
|
| 122 |
+
# Download button
|
| 123 |
st.download_button(
|
| 124 |
label="π₯ Download Report as PDF",
|
| 125 |
data=pdf_output,
|