Spaces:
Sleeping
Sleeping
noelfranthomas
commited on
Commit
•
0fb95f6
1
Parent(s):
c013750
init
Browse files- app.py +102 -0
- requirements.txt +32 -0
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Define the model and tokenizer
|
7 |
+
tokenizer_emotion = AutoTokenizer.from_pretrained("SamLowe/roberta-base-go_emotions")
|
8 |
+
model_emotion = AutoModelForSequenceClassification.from_pretrained("SamLowe/roberta-base-go_emotions")
|
9 |
+
|
10 |
+
from transformers import AutoModelForTokenClassification
|
11 |
+
|
12 |
+
tokenizer_t_class = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
|
13 |
+
|
14 |
+
model_t_class = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER")
|
15 |
+
|
16 |
+
# FastAPI app
|
17 |
+
app = FastAPI()
|
18 |
+
|
19 |
+
# Define the request body
|
20 |
+
class Input(BaseModel):
|
21 |
+
text: str
|
22 |
+
|
23 |
+
@app.post("/emotion")
|
24 |
+
async def predict_emotion(input: Input):
|
25 |
+
# Tokenize the input text
|
26 |
+
inputs = tokenizer_emotion(input.text, return_tensors="pt")
|
27 |
+
# Get the model's predictions
|
28 |
+
outputs = model_emotion(**inputs)
|
29 |
+
# Get the predicted class
|
30 |
+
predicted_class_idx = torch.argmax(outputs.logits).item()
|
31 |
+
# Decode the predicted class
|
32 |
+
predicted_class = model_emotion.config.id2label[predicted_class_idx]
|
33 |
+
# Return the prediction
|
34 |
+
return {"predicted_emotion": predicted_class}
|
35 |
+
|
36 |
+
# from transformers import pipeline
|
37 |
+
|
38 |
+
@app.post("/ner")
|
39 |
+
async def perform_ner(item: Input):
|
40 |
+
|
41 |
+
# nlp = pipeline("ner", model=model_t_class, tokenizer=tokenizer_t_class)
|
42 |
+
|
43 |
+
# ner_results = nlp(item.text)
|
44 |
+
# print(ner_results)
|
45 |
+
|
46 |
+
# Tokenize the input text
|
47 |
+
inputs = tokenizer_t_class.encode(item.text, return_tensors="pt")
|
48 |
+
# Get the model's predictions
|
49 |
+
outputs = model_t_class(inputs).logits
|
50 |
+
# Get the predicted classes for each token
|
51 |
+
predictions = torch.argmax(outputs, dim=2)
|
52 |
+
# Decode the tokens and their predicted classes
|
53 |
+
tokens = tokenizer_t_class.convert_ids_to_tokens(inputs[0])
|
54 |
+
predicted_labels = [model_t_class.config.id2label[prediction] for prediction in predictions[0].tolist()]
|
55 |
+
# Pair tokens with their predicted labels
|
56 |
+
token_labels = list(zip(tokens, predicted_labels))
|
57 |
+
# Filter out subwords
|
58 |
+
filtered_token_labels = [(token, label) for token, label in token_labels if not token.startswith("##")]
|
59 |
+
# Return the tokens and their predicted labels
|
60 |
+
return {"token_labels": filtered_token_labels}
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
# if __name__ == "__main__":
|
65 |
+
# import uvicorn
|
66 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000)
|
67 |
+
|
68 |
+
|
69 |
+
import streamlit as st
|
70 |
+
|
71 |
+
st.title("Emotion Detection App")
|
72 |
+
|
73 |
+
input_text = st.input("Enter your text here")
|
74 |
+
|
75 |
+
if st.button("Predict"):
|
76 |
+
|
77 |
+
# Tokenize the input text
|
78 |
+
inputs = tokenizer_t_class.encode(input_text, return_tensors="pt")
|
79 |
+
# Get the model's predictions
|
80 |
+
outputs = model_t_class(inputs).logits
|
81 |
+
# Get the predicted classes for each token
|
82 |
+
predictions = torch.argmax(outputs, dim=2)
|
83 |
+
# Decode the tokens and their predicted classes
|
84 |
+
tokens = tokenizer_t_class.convert_ids_to_tokens(inputs[0])
|
85 |
+
predicted_labels = [model_t_class.config.id2label[prediction] for prediction in predictions[0].tolist()]
|
86 |
+
# Pair tokens with their predicted labels
|
87 |
+
token_labels = list(zip(tokens, predicted_labels))
|
88 |
+
# Filter out subwords
|
89 |
+
filtered_token_labels = [(token, label) for token, label in token_labels if not token.startswith("##")]
|
90 |
+
# Return the tokens and their predicted labels
|
91 |
+
st.write({"token_labels": filtered_token_labels})
|
92 |
+
|
93 |
+
|
94 |
+
inputs = tokenizer_emotion(input.text, return_tensors="pt")
|
95 |
+
# Get the model's predictions
|
96 |
+
outputs = model_emotion(**inputs)
|
97 |
+
# Get the predicted class
|
98 |
+
predicted_class_idx = torch.argmax(outputs.logits).item()
|
99 |
+
# Decode the predicted class
|
100 |
+
predicted_class = model_emotion.config.id2label[predicted_class_idx]
|
101 |
+
# Return the prediction
|
102 |
+
st.write({"predicted_emotion": predicted_class})
|
requirements.txt
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
anyio==3.7.0
|
2 |
+
certifi==2023.5.7
|
3 |
+
charset-normalizer==3.1.0
|
4 |
+
click==8.1.3
|
5 |
+
exceptiongroup==1.1.1
|
6 |
+
fastapi==0.97.0
|
7 |
+
filelock==3.12.2
|
8 |
+
fsspec==2023.6.0
|
9 |
+
h11==0.14.0
|
10 |
+
huggingface-hub==0.15.1
|
11 |
+
idna==3.4
|
12 |
+
Jinja2==3.1.2
|
13 |
+
MarkupSafe==2.1.3
|
14 |
+
mpmath==1.3.0
|
15 |
+
networkx==3.1
|
16 |
+
numpy==1.25.0
|
17 |
+
packaging==23.1
|
18 |
+
pydantic==1.10.9
|
19 |
+
PyYAML==6.0
|
20 |
+
regex==2023.6.3
|
21 |
+
requests==2.31.0
|
22 |
+
safetensors==0.3.1
|
23 |
+
sniffio==1.3.0
|
24 |
+
starlette==0.27.0
|
25 |
+
sympy==1.12
|
26 |
+
tokenizers==0.13.3
|
27 |
+
torch==2.0.1
|
28 |
+
tqdm==4.65.0
|
29 |
+
transformers==4.30.2
|
30 |
+
typing_extensions==4.6.3
|
31 |
+
urllib3==2.0.3
|
32 |
+
uvicorn==0.22.0
|