aeblymt commited on
Commit
692fcc8
·
verified ·
1 Parent(s): 9b6b5a1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, Request
2
+ from starlette.middleware.cors import CORSMiddleware
3
+ import openai
4
+ import os
5
+ import requests
6
+
7
+ # Initialize FastAPI app
8
+ app = FastAPI()
9
+
10
+ # Allow CORS for Wix integration
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"], # Adjust if needed for security
14
+ allow_credentials=True,
15
+ allow_methods=["*"],
16
+ allow_headers=["*"],
17
+ )
18
+
19
+ # Load API Keys from Environment Variables
20
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
21
+ if not OPENAI_API_KEY:
22
+ raise ValueError("Missing OpenAI API Key. Set OPENAI_API_KEY in environment variables.")
23
+
24
+ # OpenAI Client Setup
25
+ openai.api_key = OPENAI_API_KEY
26
+
27
+ # Function to analyze a competitor's website
28
+ async def analyze_competitor(competitor_url: str, industry: str):
29
+ """Analyzes a competitor's website and provides AI-driven insights."""
30
+ prompt = f"""
31
+ You are an expert in competitive business analysis.
32
+ Analyze the website {competitor_url} and provide insights on:
33
+ - Website strategy (SEO, structure, content focus)
34
+ - Strengths & weaknesses in their online presence
35
+ - Estimated market positioning compared to industry standards
36
+ - Recommendations for a business competing in {industry}
37
+ """
38
+
39
+ try:
40
+ response = openai.ChatCompletion.create(
41
+ model="gpt-3.5-turbo-16k",
42
+ messages=[
43
+ {"role": "system", "content": "You are a competitive market analyst."},
44
+ {"role": "user", "content": prompt}
45
+ ],
46
+ max_tokens=2000,
47
+ temperature=0.7
48
+ )
49
+ return response["choices"][0]["message"]["content"].strip()
50
+ except Exception as e:
51
+ return f"Error generating analysis: {str(e)}"
52
+
53
+ # API Endpoint for Competitor Analysis
54
+ @app.post("/api/competitor_analysis")
55
+ async def competitor_analysis(request: Request):
56
+ """Endpoint to analyze competitor data."""
57
+ data = await request.json()
58
+ competitor_url = data.get("competitor_url")
59
+ industry = data.get("industry", "General")
60
+
61
+ if not competitor_url:
62
+ raise HTTPException(status_code=400, detail="Competitor URL is required.")
63
+
64
+ analysis = await analyze_competitor(competitor_url, industry)
65
+ return {"competitor_analysis": analysis}
66
+
67
+ # Run API (for local testing)
68
+ if __name__ == "__main__":
69
+ import uvicorn
70
+ uvicorn.run(app, host="0.0.0.0", port=7860)