Spaces:
Running
Running
Upload 3 files
Browse files- Dockerfile +20 -0
- main.py +94 -0
- requirements.txt +0 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python image from the Docker Hub
|
2 |
+
FROM python:3.11-slim
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy the requirements file to the working directory
|
8 |
+
COPY requirements.txt .
|
9 |
+
|
10 |
+
# Install the required dependencies
|
11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
12 |
+
|
13 |
+
# Copy the FastAPI application code to the container
|
14 |
+
COPY . .
|
15 |
+
|
16 |
+
# Expose the port that FastAPI will run on
|
17 |
+
EXPOSE 8000
|
18 |
+
|
19 |
+
# Command to run the FastAPI application using uvicorn
|
20 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
main.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import boto3
|
4 |
+
import json
|
5 |
+
import uuid
|
6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
7 |
+
# from mangum import Mangum
|
8 |
+
import logging
|
9 |
+
|
10 |
+
# AWS S3 Configuration
|
11 |
+
AWS_ACCESS_KEY = "AKIARZ5BMNDRSZIKZIVE"
|
12 |
+
AWS_SECRET_KEY = "dW2alIT4FYpDirGIZhHSkEINYPp30ItQCB1g/AQy"
|
13 |
+
BUCKET_NAME = "c2r-demo-requests"
|
14 |
+
BUCKET_NAME_PRICING = "c2r-website-pricing-request"
|
15 |
+
|
16 |
+
# Initialize S3 client
|
17 |
+
s3_client = boto3.client(
|
18 |
+
"s3",
|
19 |
+
aws_access_key_id=AWS_ACCESS_KEY,
|
20 |
+
aws_secret_access_key=AWS_SECRET_KEY,
|
21 |
+
)
|
22 |
+
|
23 |
+
# Initialize FastAPI
|
24 |
+
app = FastAPI()
|
25 |
+
|
26 |
+
app.add_middleware(
|
27 |
+
CORSMiddleware,
|
28 |
+
allow_origins=["*"], # Update to a specific domain in production
|
29 |
+
allow_credentials=True,
|
30 |
+
allow_methods=["*"],
|
31 |
+
allow_headers=["*"],
|
32 |
+
)
|
33 |
+
|
34 |
+
# Data Model for User Input
|
35 |
+
class UserData(BaseModel):
|
36 |
+
name: str
|
37 |
+
companyName: str
|
38 |
+
email: str
|
39 |
+
|
40 |
+
class UserDataPricing(BaseModel):
|
41 |
+
name: str
|
42 |
+
email: str
|
43 |
+
interestedInDemo : bool
|
44 |
+
|
45 |
+
|
46 |
+
logging.basicConfig(level=logging.INFO)
|
47 |
+
@app.post("/process-form")
|
48 |
+
async def process_form(data: UserData):
|
49 |
+
logging.info(f"Received data:{data}")
|
50 |
+
try:
|
51 |
+
# Generate a unique filename
|
52 |
+
filename = f"user_data_{uuid.uuid4()}.json"
|
53 |
+
|
54 |
+
# Convert user data to JSON format
|
55 |
+
json_data = json.dumps(data.dict())
|
56 |
+
|
57 |
+
# Upload JSON data to S3
|
58 |
+
s3_client.put_object(
|
59 |
+
Bucket=BUCKET_NAME,
|
60 |
+
Key=filename,
|
61 |
+
Body=json_data,
|
62 |
+
ContentType="application/json"
|
63 |
+
)
|
64 |
+
|
65 |
+
return {"message": "User data saved successfully!"}
|
66 |
+
|
67 |
+
except Exception as e:
|
68 |
+
raise HTTPException(status_code=500, detail=f"Error saving user data: {e}")
|
69 |
+
|
70 |
+
|
71 |
+
@app.post("/pricing-request")
|
72 |
+
async def process_form(data: UserDataPricing):
|
73 |
+
logging.info(f"Received data:{data}")
|
74 |
+
try:
|
75 |
+
# Generate a unique filename
|
76 |
+
filename = f"user_data_{uuid.uuid4()}.json"
|
77 |
+
|
78 |
+
# Convert user data to JSON format
|
79 |
+
json_data = json.dumps(data.dict())
|
80 |
+
|
81 |
+
# Upload JSON data to S3
|
82 |
+
s3_client.put_object(
|
83 |
+
Bucket=BUCKET_NAME_PRICING,
|
84 |
+
Key=filename,
|
85 |
+
Body=json_data,
|
86 |
+
ContentType="application/json"
|
87 |
+
)
|
88 |
+
|
89 |
+
return {"message": "User data saved successfully!"}
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
raise HTTPException(status_code=500, detail=f"Error saving user data: {e}")
|
93 |
+
# Lambda handler using Mangum
|
94 |
+
# handler = Mangum(app)
|
requirements.txt
ADDED
Binary file (900 Bytes). View file
|
|