Spaces:
Sleeping
Sleeping
Commit
·
61f1b91
1
Parent(s):
428ea02
added inference
Browse files- app.py +50 -0
- static/demo/index.html +17 -32
app.py
CHANGED
|
@@ -36,6 +36,7 @@ import logging
|
|
| 36 |
import uuid
|
| 37 |
import json
|
| 38 |
import os
|
|
|
|
| 39 |
|
| 40 |
app = Flask(__name__, static_folder='static')
|
| 41 |
|
|
@@ -122,6 +123,55 @@ def log():
|
|
| 122 |
json.dump(existing_data, f)
|
| 123 |
return jsonify({'message': 'Data added successfully', 'data': existing_data}), 201
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
if __name__=='__main__':
|
| 126 |
debug_mode = os.getenv('FLASK_DEBUG', 'True').lower() in ['true', '1', 't']
|
| 127 |
app.run(host='0.0.0.0', port='7860', debug=debug_mode)
|
|
|
|
| 36 |
import uuid
|
| 37 |
import json
|
| 38 |
import os
|
| 39 |
+
import requests
|
| 40 |
|
| 41 |
app = Flask(__name__, static_folder='static')
|
| 42 |
|
|
|
|
| 123 |
json.dump(existing_data, f)
|
| 124 |
return jsonify({'message': 'Data added successfully', 'data': existing_data}), 201
|
| 125 |
|
| 126 |
+
@app.route("/demo_inference", methods=['GET'])
|
| 127 |
+
@cross_origin()
|
| 128 |
+
def demo_inference():
|
| 129 |
+
args = request.args
|
| 130 |
+
# model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
| 131 |
+
model_id = args.get('model_id', default="meta-llama/Llama-4-Scout-17B-16E-Instruct")
|
| 132 |
+
temperature = 0.5
|
| 133 |
+
max_new_tokens = 1000
|
| 134 |
+
|
| 135 |
+
hf_token, hf_url = get_credentials.get_credentials()
|
| 136 |
+
|
| 137 |
+
prompt = args.get('prompt')
|
| 138 |
+
|
| 139 |
+
API_URL = "https://router.huggingface.co/together/v1/chat/completions"
|
| 140 |
+
headers = {
|
| 141 |
+
"Authorization": f"Bearer {hf_token}",
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
response = requests.post(
|
| 145 |
+
API_URL,
|
| 146 |
+
headers=headers,
|
| 147 |
+
json={
|
| 148 |
+
"messages": [
|
| 149 |
+
{
|
| 150 |
+
"role": "user",
|
| 151 |
+
"content": [
|
| 152 |
+
{
|
| 153 |
+
"type": "text",
|
| 154 |
+
"text": prompt
|
| 155 |
+
},
|
| 156 |
+
]
|
| 157 |
+
}
|
| 158 |
+
],
|
| 159 |
+
"model": model_id,
|
| 160 |
+
'temperature': temperature,
|
| 161 |
+
'max_new_tokens': max_new_tokens,
|
| 162 |
+
}
|
| 163 |
+
)
|
| 164 |
+
try:
|
| 165 |
+
response = response.json()["choices"][0]["message"]
|
| 166 |
+
response.update({
|
| 167 |
+
'model_id': model_id,
|
| 168 |
+
'temperature': temperature,
|
| 169 |
+
'max_new_tokens': max_new_tokens,
|
| 170 |
+
})
|
| 171 |
+
return response
|
| 172 |
+
except:
|
| 173 |
+
return response.text, response.status_code
|
| 174 |
+
|
| 175 |
if __name__=='__main__':
|
| 176 |
debug_mode = os.getenv('FLASK_DEBUG', 'True').lower() in ['true', '1', 't']
|
| 177 |
app.run(host='0.0.0.0', port='7860', debug=debug_mode)
|
static/demo/index.html
CHANGED
|
@@ -7,6 +7,8 @@
|
|
| 7 |
<link rel="stylesheet" href="https://unpkg.com/carbon-components/css/carbon-components.min.css">
|
| 8 |
<script type="text/javascript" src="static/demo/js/d3.v7.min.js"></script>
|
| 9 |
<script type="text/javascript" src="static/demo/js/jquery-3.7.1.min.js"></script>
|
|
|
|
|
|
|
| 10 |
<style type="text/css">
|
| 11 |
div.tooltip {
|
| 12 |
position: absolute;
|
|
@@ -390,22 +392,6 @@
|
|
| 390 |
|
| 391 |
// Generation request
|
| 392 |
$( "#demo" ).on( "submit", function(e){ // Hugging Face
|
| 393 |
-
|
| 394 |
-
var out = "" ;
|
| 395 |
-
const ajax_headers = {
|
| 396 |
-
'Content-Type': 'application/json',
|
| 397 |
-
'Accept': 'application/json',
|
| 398 |
-
'Access-Control-Allow-Headers': '*',
|
| 399 |
-
'Authorization' : 'Bearer <include-token-here>',
|
| 400 |
-
};
|
| 401 |
-
$.ajaxSetup({
|
| 402 |
-
headers: ajax_headers
|
| 403 |
-
});
|
| 404 |
-
|
| 405 |
-
if( ajax_headers['Authorization'] == "Bearer <include-token-here>" ){
|
| 406 |
-
console.error( "Please inform your authorization token in the ajax header setup." ) ;
|
| 407 |
-
}
|
| 408 |
-
else{
|
| 409 |
$( "#generate" ).toggleClass( "bx--btn--disabled" ) ;
|
| 410 |
( function loading_animation(){
|
| 411 |
if( $( "#outcome" ).attr( "placeholder" ) == "" ){
|
|
@@ -420,21 +406,13 @@
|
|
| 420 |
setTimeout( loading_animation, 500 );
|
| 421 |
} )()
|
| 422 |
|
| 423 |
-
var temperature = 0.5 ;
|
| 424 |
-
var max_new_tokens = 1000 ;
|
| 425 |
-
var model_id = "meta-llama/Llama-3.2-11B-Vision-Instruct"
|
| 426 |
$.ajax({
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
data: JSON.stringify({
|
| 430 |
-
"inputs": $("#prompt").val(),
|
| 431 |
-
"parameters": {
|
| 432 |
-
"temperature": temperature,
|
| 433 |
-
"max_new_tokens": max_new_tokens
|
| 434 |
-
}
|
| 435 |
-
}),
|
| 436 |
-
crossDomain: true,
|
| 437 |
success: function(data){
|
|
|
|
|
|
|
|
|
|
| 438 |
// Resetting the status of the button
|
| 439 |
$( "#generate" ).toggleClass( "bx--btn--disabled" ) ;
|
| 440 |
|
|
@@ -444,7 +422,10 @@
|
|
| 444 |
$( "#demo" ).data( "timeoutId", "" ) ;
|
| 445 |
}
|
| 446 |
|
| 447 |
-
out = data
|
|
|
|
|
|
|
|
|
|
| 448 |
|
| 449 |
$( "#outcome" ).append( "\n\n+ ------------------------------------\n| Model: " + model_id + "\n| Temperature: " + temperature + "\n| Max new tokens: " + max_new_tokens + "\n+ ------------------------------------\n\n" ) ;
|
| 450 |
// Animating the generated output
|
|
@@ -456,9 +437,13 @@
|
|
| 456 |
$( "#demo" ).data( "timeoutId", timeoutId ) ;
|
| 457 |
}
|
| 458 |
} )()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
}
|
| 460 |
-
})
|
| 461 |
-
|
| 462 |
// Returning false so the form keeps user in the same page
|
| 463 |
return false;
|
| 464 |
});
|
|
|
|
| 7 |
<link rel="stylesheet" href="https://unpkg.com/carbon-components/css/carbon-components.min.css">
|
| 8 |
<script type="text/javascript" src="static/demo/js/d3.v7.min.js"></script>
|
| 9 |
<script type="text/javascript" src="static/demo/js/jquery-3.7.1.min.js"></script>
|
| 10 |
+
<!-- <script type="text/javascript" src="js/d3.v7.min.js"></script>
|
| 11 |
+
<script type="text/javascript" src="js/jquery-3.7.1.min.js"></script> -->
|
| 12 |
<style type="text/css">
|
| 13 |
div.tooltip {
|
| 14 |
position: absolute;
|
|
|
|
| 392 |
|
| 393 |
// Generation request
|
| 394 |
$( "#demo" ).on( "submit", function(e){ // Hugging Face
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
$( "#generate" ).toggleClass( "bx--btn--disabled" ) ;
|
| 396 |
( function loading_animation(){
|
| 397 |
if( $( "#outcome" ).attr( "placeholder" ) == "" ){
|
|
|
|
| 406 |
setTimeout( loading_animation, 500 );
|
| 407 |
} )()
|
| 408 |
|
|
|
|
|
|
|
|
|
|
| 409 |
$.ajax({
|
| 410 |
+
url: encodeURI("/demo_inference?prompt=" + $("#prompt").val()),
|
| 411 |
+
dataType: 'json',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 412 |
success: function(data){
|
| 413 |
+
|
| 414 |
+
console.log("Inference response")
|
| 415 |
+
console.log(data)
|
| 416 |
// Resetting the status of the button
|
| 417 |
$( "#generate" ).toggleClass( "bx--btn--disabled" ) ;
|
| 418 |
|
|
|
|
| 422 |
$( "#demo" ).data( "timeoutId", "" ) ;
|
| 423 |
}
|
| 424 |
|
| 425 |
+
out = data.content.split("");
|
| 426 |
+
model_id = data.model_id;
|
| 427 |
+
temperature = data.temperature
|
| 428 |
+
max_new_tokens = data.max_new_tokens
|
| 429 |
|
| 430 |
$( "#outcome" ).append( "\n\n+ ------------------------------------\n| Model: " + model_id + "\n| Temperature: " + temperature + "\n| Max new tokens: " + max_new_tokens + "\n+ ------------------------------------\n\n" ) ;
|
| 431 |
// Animating the generated output
|
|
|
|
| 437 |
$( "#demo" ).data( "timeoutId", timeoutId ) ;
|
| 438 |
}
|
| 439 |
} )()
|
| 440 |
+
},
|
| 441 |
+
error: function(data) {
|
| 442 |
+
out = data.responseJSON.error.message
|
| 443 |
+
$( "#outcome" ).val(out);
|
| 444 |
}
|
| 445 |
+
})
|
| 446 |
+
|
| 447 |
// Returning false so the form keeps user in the same page
|
| 448 |
return false;
|
| 449 |
});
|