Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,36 @@
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
| 3 |
-
from langchain.chat_models import ChatOpenAI
|
| 4 |
from langchain.prompts import PromptTemplate
|
| 5 |
from langchain.chains import LLMChain
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
|
|
|
|
| 9 |
import tempfile
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
st.title("π Competitive Analysis Tool")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
st.stop()
|
| 20 |
|
| 21 |
-
# ---
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
except openai.error.OpenAIError as e:
|
| 25 |
-
st.error(f"Failed to initialize OpenAI: {str(e)}")
|
| 26 |
-
st.stop()
|
| 27 |
|
| 28 |
# --- Prompt Template ---
|
| 29 |
template = """
|
| 30 |
-
Compare the following two products
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
"""
|
| 36 |
|
| 37 |
prompt = PromptTemplate(
|
|
@@ -39,60 +38,72 @@ prompt = PromptTemplate(
|
|
| 39 |
template=template,
|
| 40 |
)
|
| 41 |
|
|
|
|
|
|
|
| 42 |
comparison_chain = LLMChain(llm=llm, prompt=prompt)
|
| 43 |
|
| 44 |
-
# --- PDF
|
| 45 |
-
def convert_to_pdf(
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def draw_feature_chart():
|
| 52 |
-
features = ["
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
fig =
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 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 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import streamlit as st
|
|
|
|
| 3 |
from langchain.prompts import PromptTemplate
|
| 4 |
from langchain.chains import LLMChain
|
| 5 |
+
from langchain_community.chat_models import ChatOpenAI
|
| 6 |
+
from openai import OpenAIError, RateLimitError, InvalidRequestError
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from fpdf import FPDF
|
| 9 |
import tempfile
|
| 10 |
|
| 11 |
+
# Set OpenAI API Key from Hugging Face secrets
|
| 12 |
+
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
|
|
|
|
| 13 |
|
| 14 |
+
# Streamlit UI
|
| 15 |
+
st.set_page_config(page_title="π Competitive Analysis Tool", layout="wide")
|
| 16 |
+
st.title("π Competitive Analysis Tool")
|
| 17 |
+
st.markdown("Compare two products or services with **Feature Comparison**, **SWOT Analysis**, and a **Summary**.")
|
|
|
|
| 18 |
|
| 19 |
+
# --- Inputs ---
|
| 20 |
+
product1 = st.text_area("πΉ Enter details about Product/Service 1", height=150)
|
| 21 |
+
product2 = st.text_area("πΉ Enter details about Product/Service 2", height=150)
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# --- Prompt Template ---
|
| 24 |
template = """
|
| 25 |
+
Compare the following two products or services:
|
| 26 |
+
[Product 1]: {product1}
|
| 27 |
+
[Product 2]: {product2}
|
| 28 |
+
|
| 29 |
+
1. Feature-by-feature comparison in a table format.
|
| 30 |
+
2. SWOT analysis for each product.
|
| 31 |
+
3. Summary highlighting key differentiators.
|
| 32 |
+
|
| 33 |
+
Be concise, clear, and professional.
|
| 34 |
"""
|
| 35 |
|
| 36 |
prompt = PromptTemplate(
|
|
|
|
| 38 |
template=template,
|
| 39 |
)
|
| 40 |
|
| 41 |
+
# --- Chain ---
|
| 42 |
+
llm = ChatOpenAI(temperature=0.2, model_name="gpt-3.5-turbo")
|
| 43 |
comparison_chain = LLMChain(llm=llm, prompt=prompt)
|
| 44 |
|
| 45 |
+
# --- PDF Converter ---
|
| 46 |
+
def convert_to_pdf(html_text):
|
| 47 |
+
pdf = FPDF()
|
| 48 |
+
pdf.add_page()
|
| 49 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 50 |
+
pdf.set_font("Arial", size=12)
|
| 51 |
+
for line in html_text.split('\n'):
|
| 52 |
+
pdf.multi_cell(0, 10, line)
|
| 53 |
+
temp_path = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf").name
|
| 54 |
+
pdf.output(temp_path)
|
| 55 |
+
return temp_path
|
| 56 |
+
|
| 57 |
+
# --- Chart Drawer (dummy feature count for visual demo) ---
|
| 58 |
def draw_feature_chart():
|
| 59 |
+
features = ["Ease of Use", "Pricing", "Customization", "Support", "Scalability"]
|
| 60 |
+
product1_scores = [7, 6, 8, 9, 7]
|
| 61 |
+
product2_scores = [8, 7, 6, 8, 9]
|
| 62 |
+
|
| 63 |
+
fig, ax = plt.subplots()
|
| 64 |
+
bar_width = 0.35
|
| 65 |
+
x = range(len(features))
|
| 66 |
+
|
| 67 |
+
ax.bar(x, product1_scores, width=bar_width, label='Product 1')
|
| 68 |
+
ax.bar([p + bar_width for p in x], product2_scores, width=bar_width, label='Product 2')
|
| 69 |
+
|
| 70 |
+
ax.set_xlabel('Features')
|
| 71 |
+
ax.set_ylabel('Score')
|
| 72 |
+
ax.set_title('Feature Comparison Chart')
|
| 73 |
+
ax.set_xticks([p + bar_width/2 for p in x])
|
| 74 |
+
ax.set_xticklabels(features)
|
| 75 |
+
ax.legend()
|
| 76 |
+
|
| 77 |
+
st.pyplot(fig)
|
| 78 |
+
|
| 79 |
+
# --- Generate Output ---
|
| 80 |
+
if st.button("βοΈ Compare Now") and product1 and product2:
|
| 81 |
+
with st.spinner("Analyzing with LLM..."):
|
| 82 |
+
try:
|
| 83 |
+
result = comparison_chain.run(product1=product1, product2=product2)
|
| 84 |
+
|
| 85 |
+
# --- Show Results ---
|
| 86 |
+
st.subheader("π Full Analysis")
|
| 87 |
+
st.markdown(result)
|
| 88 |
+
|
| 89 |
+
# --- Show Chart ---
|
| 90 |
+
st.subheader("π Feature Comparison (Chart)")
|
| 91 |
+
draw_feature_chart()
|
| 92 |
+
|
| 93 |
+
# --- Download PDF ---
|
| 94 |
+
pdf_path = convert_to_pdf(f"Competitive Analysis\n\n{result}")
|
| 95 |
+
with open(pdf_path, "rb") as f:
|
| 96 |
+
st.download_button("π Download PDF Report", f, file_name="analysis_report.pdf")
|
| 97 |
+
|
| 98 |
+
except RateLimitError:
|
| 99 |
+
st.error("π« You exceeded your OpenAI quota. Check https://platform.openai.com/account/usage")
|
| 100 |
+
|
| 101 |
+
except InvalidRequestError as e:
|
| 102 |
+
st.error(f"β Invalid request: {str(e)}")
|
| 103 |
+
|
| 104 |
+
except OpenAIError as e:
|
| 105 |
+
st.error(f"π₯ OpenAI Error: {str(e)}")
|
| 106 |
+
|
| 107 |
+
# Optional Footer
|
| 108 |
+
st.markdown("---")
|
| 109 |
+
st.markdown("Built with π‘ using Langchain, Streamlit, and OpenAI.")
|