File size: 1,795 Bytes
9ae77d3 d6f16db 26e1774 a9811ec 26e1774 f173779 26e1774 9ae77d3 26e1774 9ae77d3 6a322d3 635344d 26e1774 edfcb6a 26e1774 9ae77d3 26e1774 4393529 9ae77d3 4393529 d2df03b 9ae77d3 d2df03b 5a719df d2df03b f173779 d2df03b f0efa3b d2df03b 9ae77d3 d2df03b 18f5113 d6f16db 9ae77d3 d6f16db bf01bb4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
from flask import Flask, request, jsonify
import asyncio
import os
from hiou import hugging, browser
import json
import io
def get_answer(image: str, question: str):
filename = image
spid = os.getenv('SPID')
hug = hugging(spid)
filelink = hug.upload(filename)
image.seek(0, 2)
file_size = image.tell()
image.seek(0)
data = [
{
"meta": {
"_type": "gradio.FileData"
},
"mime_type": "image/jpeg",
"orig_name": filename,
"path": filelink.split("=", 1)[1],
"size": file_size,
"url": filelink
},
question,
{
"tab_index": 0
}
]
datas = os.getenv('DATA')
jsnd = json.loads(datas)
data.append(jsnd)
print(f"SPID : {spid}, DATA : {data}")
hug.filnal_setup(data, 2, 15)
hug.start()
return hug.output.get("data")[0]
app = Flask(__name__)
@app.route('/getans', methods=['POST'])
def async_task():
if 'file' not in request.files:
return jsonify({"status" : False, "msg" : "No file found"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"error": "No selected file"}), 400
file_content = io.BytesIO(file.read())
file_content.name = file.filename
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 = get_answer(file_content, headers.get('QU', ''))
return jsonify({"status" : True, "ANS" : answer})
@app.route('/')
def home():
return jsonify({"message": "Welcome to my Flask API!"})
|