├── README.md ├── package.json ├── client ├── src │ ├── index.js │ ├── App.css │ └── App.js ├── .gitignore ├── package.json └── public │ └── index.html └── server └── server.py /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/drnourhomsi/python_react_api/HEAD/README.md -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "bootstrap": "^5.2.3", 4 | "react-bootstrap": "^2.7.0" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /client/src/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom/client'; 3 | import App from './App'; 4 | 5 | const root = ReactDOM.createRoot(document.getElementById('root')); 6 | root.render( 7 | 8 | 9 | 10 | ) -------------------------------------------------------------------------------- /client/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /server/server.py: -------------------------------------------------------------------------------- 1 | import openai 2 | from flask import Flask, request 3 | from flask_cors import cross_origin 4 | 5 | app = Flask(__name__) 6 | 7 | @app.route("/ask", methods=["GET"]) 8 | @cross_origin() 9 | def ask(): 10 | openai.api_key = "[YOUR_API_KEY_HERE]" 11 | 12 | completions = openai.Completion.create( 13 | engine="text-davinci-003", 14 | prompt=request.args['q'], 15 | max_tokens=1024, 16 | n=1, 17 | stop=None, 18 | temperature=0.5, 19 | ) 20 | 21 | message = completions.choices[0].text 22 | return {"answers": message} 23 | 24 | if __name__ == "__main__": 25 | app.run(debug=True) -------------------------------------------------------------------------------- /client/src/App.css: -------------------------------------------------------------------------------- 1 | html, body, * { 2 | margin: 0; 3 | padding: 0; 4 | box-sizing: border-box; 5 | } 6 | 7 | body { 8 | background-color: #222; 9 | padding: 20px; 10 | } 11 | 12 | .question-form { 13 | width: 95%; 14 | position: absolute; 15 | bottom: 20px; 16 | display: flex; 17 | } 18 | 19 | .question-form input[type="text"] { 20 | width: 90%; 21 | height: 50px; 22 | border: none; 23 | padding: 15px; 24 | font-size: 1.2rem; 25 | background-color: #8e8ea0; 26 | } 27 | 28 | input[type="submit"] { 29 | height: 50px; 30 | border: none; 31 | width: 10%; 32 | } 33 | 34 | .container { 35 | background-color: black; 36 | } 37 | 38 | .answer-area { 39 | background: #343541; 40 | min-height: 70vh; 41 | padding: 15px; 42 | color: white; 43 | font-family: roboto; 44 | white-space: pre-wrap; 45 | } -------------------------------------------------------------------------------- /client/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "client", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@testing-library/jest-dom": "^5.16.5", 7 | "@testing-library/react": "^13.4.0", 8 | "@testing-library/user-event": "^13.5.0", 9 | "react": "^18.2.0", 10 | "react-dom": "^18.2.0", 11 | "react-scripts": "5.0.1", 12 | "web-vitals": "^2.1.4" 13 | }, 14 | "scripts": { 15 | "start": "react-scripts start", 16 | "build": "react-scripts build", 17 | "test": "react-scripts test", 18 | "eject": "react-scripts eject" 19 | }, 20 | "eslintConfig": { 21 | "extends": [ 22 | "react-app", 23 | "react-app/jest" 24 | ] 25 | }, 26 | "browserslist": { 27 | "production": [ 28 | ">0.2%", 29 | "not dead", 30 | "not op_mini all" 31 | ], 32 | "development": [ 33 | "last 1 chrome version", 34 | "last 1 firefox version", 35 | "last 1 safari version" 36 | ] 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /client/src/App.js: -------------------------------------------------------------------------------- 1 | import {useEffect, useState} from 'react' 2 | import './App.css' 3 | 4 | function App() { 5 | const [question, setQuestion] = useState('') 6 | const [answer, setAnswer] = useState('') 7 | const [prevAnswer, setPrevAnswer] = useState('') 8 | 9 | const handleSubmit = (e) => { 10 | e.preventDefault(); 11 | setQuestion(e.target.elements.question.value); 12 | } 13 | 14 | useEffect(()=>{ 15 | const getAnswer = async () => { 16 | let response = await fetch(`http://127.0.0.1:5000/ask?q=${question}`) 17 | response = await response.json() 18 | setAnswer(response.answers) 19 | setPrevAnswer( prev => prev +'\n\n\r'+ answer) 20 | } 21 | question !== '' && getAnswer() 22 | setQuestion(''); 23 | }, [question]); 24 | 25 | 26 | return ( 27 |
28 |
{prevAnswer}
29 | {answer} 30 |
31 |
32 | 33 | 34 |
35 |
36 | ) 37 | } 38 | 39 | export default App; -------------------------------------------------------------------------------- /client/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | --------------------------------------------------------------------------------