flaskapp / main.py
5m4ck3r's picture
Update main.py
e2ac9eb verified
raw
history blame
1.74 kB
from quart import Quart, request, jsonify
import asyncio
import os
from hiou import Ahugging, browser
import json
async def get_answer(image: str, question: str):
filename = image
spid = os.getenv('SPID')
hug = Ahugging(spid)
filelink = await hug.upload(open(filename, "rb"))
data = [
{
"meta": {
"_type": "gradio.FileData"
},
"mime_type": "image/jpeg",
"orig_name": filename,
"path": filelink.split("=", 1)[1],
"size": os.path.getsize(filename),
"url": filelink
},
question,
{
"tab_index": 0
}
]
datas = os.getenv('DATA')
jsnd = json.loads(datas)
data.append(jsnd)
print(f"SPID : {spid}, DATA : {data}")
await hug.filnal_setup(data, 2, 15)
await hug.start()
return hug.output.get("data")[0]
app = Quart(__name__)
@app.route('/getans', methods=['POST'])
async def async_task():
if 'file' not in request.files:
jsonify({"status" : False, "msg" : "No file found"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
file_content = await file.read()
headers = dict(request.headers)
if not headers.get("KEY") == os.getenv("KEY"):
return jsonify({"status" : False, "msg" : "Invalid API Key"}), 404
print(f"QUESTION ASKED : {headers.get('QU', '')}")
answer = await get_answer(file_content, headers.get('QU', ''))
return jsonify({"status" : True, "ANS" : answer})
@app.route('/')
async def home():
return jsonify({"message": "Welcome to my Flask API!"})