|
from flask import Flask, request, jsonify |
|
import asyncio |
|
import os |
|
from hiou import hugging, browser |
|
import json |
|
|
|
def get_answer(image: str, question: str): |
|
filename = image |
|
spid = os.getenv('SPID') |
|
hug = hugging(spid) |
|
filelink = hug.upload(filename) |
|
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}") |
|
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: |
|
jsonify({"status" : False, "msg" : "No file found"}), 400 |
|
file = request.files['file'] |
|
if file.filename == '': |
|
return jsonify({"error": "No selected file"}), 400 |
|
file_content = file |
|
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!"}) |
|
|
|
|