Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask,render_template,request,jsonify
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
|
12 |
+
OPENAI_API_KEY=os.getenv("OPENAI_API_KEY")
|
13 |
+
client = openai.Client(api_key=OPENAI_API_KEY)
|
14 |
+
|
15 |
+
def get_response(prompt):
|
16 |
+
stm = client.chat.completions.create(
|
17 |
+
model="gpt-4o-mini",
|
18 |
+
messages=[{"role": "user", "content": prompt}],
|
19 |
+
stream=False,
|
20 |
+
)
|
21 |
+
res=stm.choices[0].message.content
|
22 |
+
return res
|
23 |
+
|
24 |
+
|
25 |
+
app = Flask(__name__)
|
26 |
+
|
27 |
+
@app.route('/')
|
28 |
+
def index():
|
29 |
+
return render_template("index.html")
|
30 |
+
|
31 |
+
@app.route('/api/process_text', methods=['POST'])
|
32 |
+
def process_text():
|
33 |
+
data = request.get_json()
|
34 |
+
text = data['text']
|
35 |
+
print(text)
|
36 |
+
|
37 |
+
processed_text = text.lower()
|
38 |
+
|
39 |
+
response = get_response(processed_text)
|
40 |
+
print(response)
|
41 |
+
|
42 |
+
return jsonify(response)
|
43 |
+
|
44 |
+
if __name__ == '__main__':
|
45 |
+
app.run(debug=True)
|