Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,103 +2,94 @@ import os
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain.prompts import PromptTemplate
|
| 4 |
from langchain.chains import LLMChain
|
| 5 |
-
from langchain_community.
|
| 6 |
-
|
| 7 |
-
import
|
| 8 |
-
from
|
| 9 |
-
import
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
os.environ["
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
Product 1: {product1}
|
| 21 |
-
|
| 22 |
Product 2: {product2}
|
| 23 |
|
| 24 |
-
1.
|
| 25 |
-
2.
|
| 26 |
-
3.
|
|
|
|
|
|
|
| 27 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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 |
-
# PDF Download
|
| 90 |
-
pdf_path = convert_to_pdf(f"Competitive Analysis\n\n{result}")
|
| 91 |
-
with open(pdf_path, "rb") as f:
|
| 92 |
-
st.download_button("π Download PDF Report", f, file_name="analysis_report.pdf")
|
| 93 |
-
|
| 94 |
-
except openai.RateLimitError:
|
| 95 |
-
st.error("π« You exceeded your OpenAI quota. Check https://platform.openai.com/account/usage")
|
| 96 |
-
|
| 97 |
-
except openai.InvalidRequestError as e:
|
| 98 |
-
st.error(f"β Invalid request: {str(e)}")
|
| 99 |
-
|
| 100 |
-
except openai.OpenAIError as e:
|
| 101 |
-
st.error(f"π₯ OpenAI Error: {str(e)}")
|
| 102 |
-
|
| 103 |
-
except Exception as e:
|
| 104 |
-
st.error(f"β οΈ Unexpected Error: {str(e)}")
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from langchain.prompts import PromptTemplate
|
| 4 |
from langchain.chains import LLMChain
|
| 5 |
+
from langchain_community.llms import HuggingFaceHub
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
from reportlab.pdfgen import canvas
|
| 9 |
+
from reportlab.lib.pagesizes import letter
|
| 10 |
+
|
| 11 |
+
# Load Hugging Face token
|
| 12 |
+
os.environ["HUGGINGFACEHUB_API_TOKEN"] = st.secrets["HF_TOKEN"]
|
| 13 |
+
|
| 14 |
+
# Initialize LLM
|
| 15 |
+
llm = HuggingFaceHub(
|
| 16 |
+
repo_id="google/flan-t5-xl",
|
| 17 |
+
model_kwargs={"temperature": 0.5, "max_length": 1024}
|
| 18 |
+
)
|
| 19 |
|
| 20 |
+
# Prompt Template
|
| 21 |
+
template = """
|
| 22 |
+
Compare the following two products or services:
|
| 23 |
Product 1: {product1}
|
|
|
|
| 24 |
Product 2: {product2}
|
| 25 |
|
| 26 |
+
1. Feature-by-feature comparison.
|
| 27 |
+
2. SWOT analysis for each.
|
| 28 |
+
3. A comparative summary highlighting key differentiators.
|
| 29 |
+
|
| 30 |
+
Respond in structured format with headings.
|
| 31 |
"""
|
| 32 |
+
|
| 33 |
+
prompt = PromptTemplate(
|
| 34 |
+
input_variables=["product1", "product2"],
|
| 35 |
+
template=template,
|
| 36 |
)
|
| 37 |
|
| 38 |
+
comparison_chain = LLMChain(llm=llm, prompt=prompt)
|
| 39 |
+
|
| 40 |
+
# Streamlit UI
|
| 41 |
+
st.title("π Competitive Analysis Tool (Hugging Face Version)")
|
| 42 |
+
|
| 43 |
+
product1 = st.text_area("Enter details for Product/Service 1")
|
| 44 |
+
product2 = st.text_area("Enter details for Product/Service 2")
|
| 45 |
+
|
| 46 |
+
if st.button("Compare"):
|
| 47 |
+
with st.spinner("Generating analysis..."):
|
| 48 |
+
try:
|
| 49 |
+
result = comparison_chain.run(product1=product1, product2=product2)
|
| 50 |
+
st.subheader("π Analysis Result")
|
| 51 |
+
st.markdown(result)
|
| 52 |
+
|
| 53 |
+
# Optional: Simple chart visualization (Dummy feature scores)
|
| 54 |
+
features = ["Usability", "Performance", "Support", "Integration"]
|
| 55 |
+
p1_scores = [8, 7, 6, 9]
|
| 56 |
+
p2_scores = [7, 8, 8, 7]
|
| 57 |
+
|
| 58 |
+
fig, ax = plt.subplots()
|
| 59 |
+
bar_width = 0.35
|
| 60 |
+
index = range(len(features))
|
| 61 |
+
|
| 62 |
+
ax.bar(index, p1_scores, bar_width, label="Product 1")
|
| 63 |
+
ax.bar([i + bar_width for i in index], p2_scores, bar_width, label="Product 2")
|
| 64 |
+
|
| 65 |
+
ax.set_xlabel('Features')
|
| 66 |
+
ax.set_ylabel('Scores')
|
| 67 |
+
ax.set_title('Feature Comparison')
|
| 68 |
+
ax.set_xticks([i + bar_width / 2 for i in index])
|
| 69 |
+
ax.set_xticklabels(features)
|
| 70 |
+
ax.legend()
|
| 71 |
+
|
| 72 |
+
st.pyplot(fig)
|
| 73 |
+
|
| 74 |
+
# PDF Download
|
| 75 |
+
def create_pdf(text):
|
| 76 |
+
buffer = BytesIO()
|
| 77 |
+
c = canvas.Canvas(buffer, pagesize=letter)
|
| 78 |
+
width, height = letter
|
| 79 |
+
y = height - 40
|
| 80 |
+
|
| 81 |
+
for line in text.split('\n'):
|
| 82 |
+
c.drawString(30, y, line)
|
| 83 |
+
y -= 15
|
| 84 |
+
if y < 50:
|
| 85 |
+
c.showPage()
|
| 86 |
+
y = height - 40
|
| 87 |
+
c.save()
|
| 88 |
+
buffer.seek(0)
|
| 89 |
+
return buffer
|
| 90 |
+
|
| 91 |
+
pdf_data = create_pdf(result)
|
| 92 |
+
st.download_button("π Download PDF Report", data=pdf_data, file_name="analysis_report.pdf")
|
| 93 |
+
|
| 94 |
+
except Exception as e:
|
| 95 |
+
st.error(f"Something went wrong: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|