|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
Flask API app and routes. |
|
""" |
|
|
|
__author__ = "Vagner Santana, Melina Alberio, Cassia Sanctos and Tiago Machado" |
|
__copyright__ = "IBM Corporation 2024" |
|
__credits__ = ["Vagner Santana, Melina Alberio, Cassia Sanctos, Tiago Machado"] |
|
__license__ = "Apache 2.0" |
|
__version__ = "0.0.1" |
|
|
|
|
|
from flask import Flask, request, jsonify |
|
from flask_cors import CORS, cross_origin |
|
from flask_restful import Resource, Api, reqparse |
|
import control.recommendation_handler as recommendation_handler |
|
from helpers import get_credentials, authenticate_api, save_model |
|
import config as cfg |
|
import requests |
|
import logging |
|
import uuid |
|
import json |
|
import os |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
logging.basicConfig( |
|
filename='app.log', |
|
level=logging.INFO, |
|
format='%(asctime)s - %(levelname)s - %(message)s' |
|
) |
|
|
|
|
|
logger = app.logger |
|
|
|
id = str(uuid.uuid4()) |
|
|
|
|
|
app.register_blueprint(cfg.SWAGGER_BLUEPRINT, url_prefix = cfg.SWAGGER_URL) |
|
FRONT_LOG_FILE = 'front_log.json' |
|
|
|
|
|
@app.route("/") |
|
def index(): |
|
user_ip = request.remote_addr |
|
logger.info(f'USER {user_ip} - ID {id} - started the app') |
|
return app.send_static_file('demo/index.html') |
|
|
|
@app.route("/recommend", methods=['GET']) |
|
@cross_origin() |
|
def recommend(): |
|
user_ip = request.remote_addr |
|
hf_token, hf_url = get_credentials.get_credentials() |
|
api_url, headers = authenticate_api.authenticate_api(hf_token, hf_url) |
|
prompt_json = recommendation_handler.populate_json() |
|
args = request.args |
|
prompt = args.get("prompt") |
|
print(prompt) |
|
recommendation_json = recommendation_handler.recommend_prompt(prompt, prompt_json, |
|
api_url, headers) |
|
logger.info(f'USER - {user_ip} - ID {id} - accessed recommend route') |
|
logger.info(f'RECOMMEND ROUTE - request: {prompt} response: {recommendation_json}') |
|
return recommendation_json |
|
|
|
@app.route("/get_thresholds", methods=['GET']) |
|
@cross_origin() |
|
def get_thresholds(): |
|
hf_token, hf_url = get_credentials.get_credentials() |
|
api_url, headers = authenticate_api.authenticate_api(hf_token, hf_url) |
|
prompt_json = recommendation_handler.populate_json() |
|
model_id = 'sentence-transformers/all-minilm-l6-v2' |
|
args = request.args |
|
|
|
prompt = args.get("prompt") |
|
thresholds_json = recommendation_handler.get_thresholds(prompt, prompt_json, api_url, |
|
headers, model_id) |
|
return thresholds_json |
|
|
|
@app.route("/recommend_local", methods=['GET']) |
|
@cross_origin() |
|
def recommend_local(): |
|
model_id, model_path = save_model.save_model() |
|
prompt_json = recommendation_handler.populate_json() |
|
args = request.args |
|
print("args list = ", args) |
|
prompt = args.get("prompt") |
|
local_recommendation_json = recommendation_handler.recommend_local(prompt, prompt_json, |
|
model_id, model_path) |
|
return local_recommendation_json |
|
|
|
@app.route("/log", methods=['POST']) |
|
@cross_origin() |
|
def log(): |
|
f_path = 'static/demo/log/' |
|
new_data = request.get_json() |
|
|
|
try: |
|
with open(f_path+FRONT_LOG_FILE, 'r') as f: |
|
existing_data = json.load(f) |
|
except FileNotFoundError: |
|
existing_data = [] |
|
|
|
existing_data.update(new_data) |
|
|
|
|
|
with open(f_path+FRONT_LOG_FILE, 'w') as f: |
|
json.dump(existing_data, f) |
|
return jsonify({'message': 'Data added successfully', 'data': existing_data}), 201 |
|
|
|
@app.route("/demo_inference", methods=['GET']) |
|
@cross_origin() |
|
def demo_inference(): |
|
args = request.args |
|
|
|
model_id = args.get('model_id', default="meta-llama/Llama-4-Scout-17B-16E-Instruct") |
|
temperature = args.get('temperature', default=0.5) |
|
max_new_tokens = args.get('max_new_tokens', default=1000) |
|
|
|
return { |
|
'content': """ |
|
To effectively support your demands for increased resources, you'll want to gather a combination of quantitative and qualitative evidence. Here's a list of items you might consider compiling: |
|
|
|
1. **Project backlog and pipeline:** Show the number of projects currently in the pipeline and those waiting to be started. This can help demonstrate the demand for your team's services. |
|
|
|
2. **Project completion rate:** Calculate the percentage of projects completed on time and within budget. This can help show the efficiency of your team and the potential for scaling up without significantly impacting project quality. |
|
|
|
3. **Client satisfaction data:** Collect feedback from clients, such as Net Promoter Score (NPS), survey responses, or testimonials. This can help demonstrate the value your team provides and the potential for acquiring new clients through word-of-mouth referrals. |
|
|
|
4. **User engagement metrics:** Gather data on user engagement from your landing pages and UX interfaces, such as click-through rates, conversion rates, and bounce rates. This can help show the effectiveness of your designs and the potential for improved results with a larger team. |
|
|
|
5. **Average project timeline:** Calculate the average time it takes for a project to be completed from start to finish. This can help demonstrate the need for more resources to meet increasing demand and maintain a reasonable project turnaround time. |
|
|
|
6. **Resource utilization:** Analyze the current workload distribution among team members to identify bottlenecks and areas where additional resources could improve efficiency. |
|
""", |
|
'model_id':model_id, |
|
'temperature': temperature, |
|
'max_new_tokens': max_new_tokens |
|
} |
|
|
|
hf_token, _ = get_credentials.get_credentials() |
|
|
|
prompt = args.get('prompt') |
|
|
|
API_URL = "https://router.huggingface.co/together/v1/chat/completions" |
|
headers = { |
|
"Authorization": f"Bearer {hf_token}", |
|
} |
|
|
|
response = requests.post( |
|
API_URL, |
|
headers=headers, |
|
json={ |
|
"messages": [ |
|
{ |
|
"role": "user", |
|
"content": [ |
|
{ |
|
"type": "text", |
|
"text": prompt |
|
}, |
|
] |
|
} |
|
], |
|
"model": model_id, |
|
'temperature': temperature, |
|
'max_new_tokens': max_new_tokens, |
|
} |
|
) |
|
try: |
|
response = response.json()["choices"][0]["message"] |
|
response.update({ |
|
'model_id': model_id, |
|
'temperature': temperature, |
|
'max_new_tokens': max_new_tokens, |
|
}) |
|
return response |
|
except: |
|
return response.text, response.status_code |
|
|
|
if __name__=='__main__': |
|
debug_mode = os.getenv('FLASK_DEBUG', 'True').lower() in ['true', '1', 't'] |
|
app.run(host='0.0.0.0', port='8080', debug=debug_mode) |