diff --git a/cloud-functions/python/functions/curvefit/main.py b/cloud-functions/python/functions/curvefit/main.py new file mode 100644 index 0000000..128639e --- /dev/null +++ b/cloud-functions/python/functions/curvefit/main.py @@ -0,0 +1,75 @@ +import functions_framework +from flask import Flask, jsonify, send_file +from flask_cors import CORS +import matplotlib.pyplot as plt +from matplotlib.backends.backend_svg import FigureCanvasSVG +import io +import os +import torch + +app = Flask(__name__) +CORS(app) + +def create_svg_plot(): + weight = 0.7 + bias = 0.3 + + start = 0 + end = 1 + step = 0.02 + X = torch.arange(start, end, step).unsqueeze(dim=1) + Y = weight * X + bias + # print(Y) + + x = [1, 2, 3, 4, 5] + y = [2, 4, 6, 8, 10] + + fig, ax = plt.subplots() + ax.plot(x, y) + ax.set_title('Sample Plot') + ax.set_xlabel('X-axis') + ax.set_ylabel('Y-axis') + + svg_output = io.StringIO() + canvas = FigureCanvasSVG(fig) + canvas.print_svg(svg_output) + + # Close the plot to free up resources + plt.close(fig) + + return svg_output.getvalue() + +@functions_framework.http +def curvefit(request): + """HTTP Cloud Function. + Args: + request (flask.Request): The request object. + + Returns: + The response text, or any set of values that can be turned into a + Response object using `make_response` + . + """ + if request.method == 'OPTIONS': + headers = { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', + 'Access-Control-Allow-Headers': 'Content-Type', + # 'Access-Control-Max-Age': '3600' + } + + return ('', 204, headers) + + headers = { + 'Access-Control-Allow-Origin': '*' + } + + svg_plot = create_svg_plot() + + output_path = os.getcwd() + "/" + "plot.svg" + + with open(output_path,'rb') as f: + file_data = io.BytesIO(f.read()) + + # return (jsonify({"plot": svg_plot}), 200, headers) + return send_file(file_data, as_attachment=False, download_name= "plot.svg", mimetype='image/svg+xml') diff --git a/cloud-functions/python/functions/curvefit/requirements.txt b/cloud-functions/python/functions/curvefit/requirements.txt new file mode 100644 index 0000000..7f40231 --- /dev/null +++ b/cloud-functions/python/functions/curvefit/requirements.txt @@ -0,0 +1,10 @@ +functions-framework==3.* +firebase_functions~=0.1.0 +pandas==2.0.3 +openpyxl==3.1.2 +firebase-admin==6.2.0 +scikit-learn==1.3.1 +matplotlib==3.8.0 +torch==2.1.0 +torchaudio==2.1.0 +torchvision==0.16.0 \ No newline at end of file