CRYSTAL-R1 / api_host.py
crystal-technologies's picture
Update api_host.py
dd96d5f
raw
history blame
3.46 kB
from Perceptrix.engine import perceptrix, robotix, identify_objects_from_text, search_keyword
from CircumSpect import answer_question, find_object_description, locate_object
from flask import Flask, request, jsonify
import numpy as np
import threading
import whisper
import cv2
import os
model = whisper.load_model("base")
def transcribe(audio):
result = model.transcribe(audio)
transcription = result['text']
print(transcription)
return transcription
app = Flask(__name__)
@app.route('/locate_object', methods=['POST'])
def display_image():
try:
image_data = request.json['image_data']
prompt = request.json['prompt']
image_data = np.array(image_data, dtype=np.uint8)
image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
cv2.imwrite('API.jpg', image)
answer, annotated_image = locate_object(prompt, "API.jpg")
return jsonify({'message': answer, 'annotated_image': annotated_image})
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/vqa', methods=['POST'])
def display_image():
try:
image_data = request.json['image_data']
prompt = request.json['prompt']
image_data = np.array(image_data, dtype=np.uint8)
image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
cv2.imwrite('API.jpg', image)
answer = answer_question(prompt, "API.jpg")
return jsonify({'message': answer})
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/object_description', methods=['POST'])
def display_image():
try:
image_data = request.json['image_data']
image_data = np.array(image_data, dtype=np.uint8)
image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
cv2.imwrite('API.jpg', image)
answer = find_object_description("API.jpg")
return jsonify({'message': answer})
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/perceptrix', methods=['POST'])
def display_image():
try:
prompt = request.json['prompt']
answer = perceptrix(prompt)
return jsonify({'message': answer})
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/robotix', methods=['POST'])
def display_image():
try:
prompt = request.json['prompt']
answer = robotix(prompt)
return jsonify({'message': answer})
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/search_keyword', methods=['POST'])
def display_image():
try:
prompt = request.json['prompt']
answer = search_keyword(prompt)
return jsonify({'message': answer})
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/identify_objects_from_text', methods=['POST'])
def display_image():
try:
prompt = request.json['prompt']
answer = identify_objects_from_text(prompt)
return jsonify({'message': answer})
except Exception as e:
return jsonify({'error': str(e)})
@app.route('/transcribe', methods=['POST'])
def upload_audio():
audio_file = request.files['audio']
filename = os.path.join("database", audio_file.filename)
audio_file.save(filename)
return jsonify({'message': transcribe(filename)})
def run_app():
app.run(port=7777)
if __name__ == "__main__":
runner = threading.Thread(target=run_app)
runner.start()