Spaces:
Sleeping
Sleeping
Alejadro Sanchez-Giraldo
commited on
Commit
•
0c868d2
1
Parent(s):
77d5f1f
push changes for api
Browse files
README.md
CHANGED
@@ -16,6 +16,14 @@ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-
|
|
16 |
|
17 |
## Run on docker
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
docker run -it -p 8501:8501 --platform=linux/amd64 \
|
20 |
-e LAUNCHDARKLY_SDK_KEY="sdk-142d656c-d430-4f8c-b2f1-7275f2ec65ff" \
|
21 |
-
registry.hf.space/asgface-sentimentai:latest
|
|
|
16 |
|
17 |
## Run on docker
|
18 |
|
19 |
+
### UI
|
20 |
+
|
21 |
+
docker run -it -p 8501:8501 --platform=linux/amd64 \
|
22 |
+
-e LAUNCHDARKLY_SDK_KEY="sdk-142d656c-d430-4f8c-b2f1-7275f2ec65ff" \
|
23 |
+
registry.hf.space/asgface-sentimentai:latest streamlit run app.py
|
24 |
+
|
25 |
+
### API
|
26 |
+
|
27 |
docker run -it -p 8501:8501 --platform=linux/amd64 \
|
28 |
-e LAUNCHDARKLY_SDK_KEY="sdk-142d656c-d430-4f8c-b2f1-7275f2ec65ff" \
|
29 |
+
registry.hf.space/asgface-sentimentai:latest python api.py
|
api.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import streamlit as st
|
3 |
+
from transformers import pipeline
|
4 |
+
import os
|
5 |
+
from ldclient import LDClient, Config, Context
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
# Retrieve the LaunchDarkly SDK key from environment variables
|
10 |
+
ld_sdk_key = os.getenv("LAUNCHDARKLY_SDK_KEY")
|
11 |
+
|
12 |
+
# Initialize LaunchDarkly client with the correct configuration
|
13 |
+
ld_client = LDClient(Config(ld_sdk_key))
|
14 |
+
|
15 |
+
# Function to get the AI model configuration from LaunchDarkly
|
16 |
+
|
17 |
+
|
18 |
+
def get_model_config(user_name):
|
19 |
+
flag_key = "model-swap" # Replace with your flag key
|
20 |
+
|
21 |
+
# Create a context using Context Builder—it can be anything, but for this use case, I’m just defaulting to myself.
|
22 |
+
|
23 |
+
context = Context.builder(
|
24 |
+
f"context-key-{user_name}").name(user_name).build()
|
25 |
+
flag_variation = ld_client.variation(flag_key, context, default={})
|
26 |
+
|
27 |
+
model_id = flag_variation.get("modelID", "distilbert-base-uncased")
|
28 |
+
return model_id
|
29 |
+
|
30 |
+
# Function to translate sentiment labels to user-friendly terms
|
31 |
+
|
32 |
+
|
33 |
+
def translate_label(label):
|
34 |
+
label_mapping = {
|
35 |
+
"LABEL_0": "🤬 Negative",
|
36 |
+
"LABEL_1": "😶 Neutral",
|
37 |
+
"LABEL_2": "😃 Positive",
|
38 |
+
"1 star": "🤬 Negative",
|
39 |
+
"2 stars": "🤬 Negative",
|
40 |
+
"3 stars": "😶 Neutral",
|
41 |
+
"4 stars": "😃 Positive",
|
42 |
+
"5 stars": "😃 Positive"
|
43 |
+
}
|
44 |
+
return label_mapping.get(label, "Unknown")
|
45 |
+
|
46 |
+
|
47 |
+
@app.route('/analyze', methods=['POST'])
|
48 |
+
def analyze_sentiment():
|
49 |
+
data = request.json
|
50 |
+
name = data.get('name', 'Anonymous')
|
51 |
+
user_input = data.get('text', '')
|
52 |
+
|
53 |
+
if not user_input:
|
54 |
+
return jsonify({"error": "No text provided for analysis"}), 400
|
55 |
+
|
56 |
+
model_id = get_model_config(name)
|
57 |
+
model = pipeline("sentiment-analysis", model=model_id)
|
58 |
+
|
59 |
+
results = model(user_input)
|
60 |
+
translated_results = [{"Sentiment": translate_label(
|
61 |
+
result['label']), "Confidence": result['score'], "User_input": user_input} for result in results]
|
62 |
+
|
63 |
+
return jsonify({"name": name, "results": translated_results})
|
64 |
+
|
65 |
+
|
66 |
+
if __name__ == '__main__':
|
67 |
+
app.run(debug=True)
|
app.py
CHANGED
@@ -2,7 +2,6 @@ import streamlit as st
|
|
2 |
from transformers import pipeline
|
3 |
from ldclient import LDClient, Config, Context
|
4 |
import os
|
5 |
-
import torch
|
6 |
|
7 |
# Retrieve the LaunchDarkly SDK key from environment variables
|
8 |
ld_sdk_key = os.getenv("LAUNCHDARKLY_SDK_KEY")
|
|
|
2 |
from transformers import pipeline
|
3 |
from ldclient import LDClient, Config, Context
|
4 |
import os
|
|
|
5 |
|
6 |
# Retrieve the LaunchDarkly SDK key from environment variables
|
7 |
ld_sdk_key = os.getenv("LAUNCHDARKLY_SDK_KEY")
|
requirements.txt
CHANGED
@@ -2,3 +2,4 @@ streamlit
|
|
2 |
transformers
|
3 |
launchdarkly-server-sdk
|
4 |
torch
|
|
|
|
2 |
transformers
|
3 |
launchdarkly-server-sdk
|
4 |
torch
|
5 |
+
Flask
|