Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,36 @@
|
|
1 |
-
|
|
|
2 |
import openai
|
3 |
|
4 |
-
|
5 |
-
app = FastAPI()
|
6 |
|
7 |
-
#
|
8 |
openai.api_key = "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"
|
9 |
|
10 |
-
@app.
|
11 |
-
def
|
12 |
-
return
|
13 |
|
14 |
-
@app.
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
try:
|
17 |
-
response = openai.
|
18 |
-
|
19 |
-
|
|
|
20 |
)
|
21 |
-
|
|
|
|
|
|
|
22 |
except Exception as e:
|
23 |
-
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
from flask import Flask, request, jsonify
|
3 |
import openai
|
4 |
|
5 |
+
app = Flask(__name__)
|
|
|
6 |
|
7 |
+
# API Key yahan enter karo
|
8 |
openai.api_key = "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"
|
9 |
|
10 |
+
@app.route("/", methods=["GET"])
|
11 |
+
def home():
|
12 |
+
return "<h1>ChatGPT Query Service</h1><p>Enter your query at /query endpoint.</p>"
|
13 |
|
14 |
+
@app.route("/query", methods=["POST"])
|
15 |
+
def query():
|
16 |
+
data = request.get_json()
|
17 |
+
query_text = data.get("query", "")
|
18 |
+
|
19 |
+
if not query_text:
|
20 |
+
return jsonify({"error": "Query text is required"}), 400
|
21 |
+
|
22 |
try:
|
23 |
+
response = openai.Completion.create(
|
24 |
+
engine="text-davinci-003",
|
25 |
+
prompt=query_text,
|
26 |
+
max_tokens=100
|
27 |
)
|
28 |
+
|
29 |
+
answer = response.choices[0].text.strip()
|
30 |
+
return jsonify({"query": query_text, "answer": answer})
|
31 |
+
|
32 |
except Exception as e:
|
33 |
+
return jsonify({"error": str(e)}), 500
|
34 |
+
|
35 |
+
if __name__ == "__main__":
|
36 |
+
app.run(host="0.0.0.0", port=8000)
|