diff --git a/Backend/backend.py b/Backend/backend.py index 7f1f7c3..b22f41a 100644 --- a/Backend/backend.py +++ b/Backend/backend.py @@ -22,7 +22,17 @@ "http://127.0.0.1:5001" # This is where db_access.py is running. ) +@app.route("/generate_image", methods=["POST"]) +def generate_image(): + data = request.get_json() + url = "https://imagegolf.io/api/generate" + url_data = { + "inputValue": data["prompt"] + } + + r = requests.post(url, json=url_data) + return r.json() @app.route("/store_image", methods=["POST"]) def store_image(): @@ -94,7 +104,7 @@ def login(): @app.route("/create_user", methods=["POST"]) -def register(): +def create_user(): print("received register request") print(request, request.data) return jsonify({"message": "No endpoint called create_user, perhaps you meant: /register"}) @@ -147,8 +157,6 @@ def register(): return jsonify({"message": "Failed to create user!!"}) - - if __name__ == "__main__": try: app.run(port=5000, debug=True) diff --git a/Backend/generate_url.py b/Backend/generate_url.py deleted file mode 100644 index 50fb18a..0000000 --- a/Backend/generate_url.py +++ /dev/null @@ -1,22 +0,0 @@ -import requests -import sys - - -def generate_url(prompt): - url = "https://imagegolf.io/api/generate" - data = { - "inputValue": prompt - } - - r = requests.post(url, json=data) - - return (r.json()["output"]) - - -if __name__ == "__main__": - if len(sys.argv) < 2: - print("Usage: script_name ") - sys.exit(1) - input_value = sys.argv[1] - result = generate_url(input_value) - print(result) diff --git a/Frontend/src/pages/api/generateURL.js b/Frontend/src/pages/api/generateURL.js deleted file mode 100644 index 191e3ea..0000000 --- a/Frontend/src/pages/api/generateURL.js +++ /dev/null @@ -1,24 +0,0 @@ -// pages/api/generateURL.js -import { exec } from "child_process"; - -export default function handler(req, res) { - if (req.method !== "POST") { - return res.status(405).end(); - } - - const inputValue = req.body.inputValue; - - exec( - `python3 ../Backend/generate_url.py "${inputValue}"`, - (error, stdout, stderr) => { - if (error) { - console.error(`exec error: ${error}`); - return res - .status(500) - .json({ error: "Failed to execute python script." }); - } - - res.json({ url: stdout }); - } - ); -} diff --git a/Frontend/src/pages/create.js b/Frontend/src/pages/create.js index daba350..d02c2c8 100644 --- a/Frontend/src/pages/create.js +++ b/Frontend/src/pages/create.js @@ -1,4 +1,5 @@ import React, { useState } from "react"; +import axios from "axios"; import Image from "next/image"; export default function Home() { @@ -10,23 +11,16 @@ export default function Home() { e.preventDefault(); try { - const response = await fetch("/api/generateURL", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - inputValue: text, - }), - }); + const response = await axios.post( + "http://localhost:5000/generate_image", + {"prompt": text} + ); - const data = await response.json(); - - if (data.url) { - setUrl(data.url); + if (response.data.output) { + setUrl(response.data.output); setImageAccepted(null); // Reset the decision state when a new image is fetched - } else { - console.error("Failed to get URL."); + } else if (response.data.error) { + console.error(response.data.error); } } catch (error) { console.error("Error:", error);