rapid12k4 commited on
Commit
8e966cd
1 Parent(s): b98a8d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -1,23 +1,36 @@
1
- from fastapi import FastAPI, HTTPException
 
2
  import openai
3
 
4
- # Initialize the FastAPI app
5
- app = FastAPI()
6
 
7
- # Set your OpenAI API key directly here
8
  openai.api_key = "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"
9
 
10
- @app.get("/")
11
- def greet_json():
12
- return {"Hello": "World!"}
13
 
14
- @app.post("/chat")
15
- async def chat_with_gpt(prompt: str):
 
 
 
 
 
 
16
  try:
17
- response = openai.ChatCompletion.create(
18
- model="gpt-3.5-turbo",
19
- messages=[{"role": "user", "content": prompt}]
 
20
  )
21
- return {"response": response.choices[0].message['content']}
 
 
 
22
  except Exception as e:
23
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
 
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)