Spaces:
Runtime error
Runtime error
QuophyDzifa
commited on
Commit
•
10be56e
1
Parent(s):
23bc11b
add application file
Browse files
app.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
# """gradio_app.ipynb
|
3 |
+
|
4 |
+
# Automatically generated by Colaboratory.
|
5 |
+
|
6 |
+
# Original file is located at
|
7 |
+
# https://colab.research.google.com/drive/1u8oKw0KTptVWpY-cKFL87N2IDDrM4lTc
|
8 |
+
# """
|
9 |
+
|
10 |
+
|
11 |
+
import gradio as gr
|
12 |
+
import pandas as pd
|
13 |
+
import numpy as np
|
14 |
+
import pickle
|
15 |
+
from scipy.special import softmax
|
16 |
+
from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig
|
17 |
+
|
18 |
+
|
19 |
+
# Requirements
|
20 |
+
model_path = "QuophyDzifa/Sentiment-Analysis-Model"
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
22 |
+
config = AutoConfig.from_pretrained(model_path)
|
23 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_path)
|
24 |
+
|
25 |
+
|
26 |
+
# Preprocess text (username and link placeholders)
|
27 |
+
def preprocess(text):
|
28 |
+
new_text = []
|
29 |
+
for t in text.split(" "):
|
30 |
+
t = '@user' if t.startswith('@') and len(t) > 1 else t
|
31 |
+
t = 'http' if t.startswith('http') else t
|
32 |
+
new_text.append(t)
|
33 |
+
return " ".join(new_text)
|
34 |
+
|
35 |
+
|
36 |
+
def sent_analysis(text):
|
37 |
+
text = preprocess(text)
|
38 |
+
|
39 |
+
# PyTorch-based models
|
40 |
+
encoded_input = tokenizer(text, return_tensors='pt')
|
41 |
+
output = model(**encoded_input)
|
42 |
+
scores_ = output[0][0].detach().numpy()
|
43 |
+
scores_ = softmax(scores_)
|
44 |
+
|
45 |
+
# Format output dict of scores
|
46 |
+
labels = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
|
47 |
+
scores = {labels[i]: float(s) for i, s in enumerate(scores_)}
|
48 |
+
return scores
|
49 |
+
|
50 |
+
|
51 |
+
demo = gr.Interface(
|
52 |
+
fn=sent_analysis,
|
53 |
+
inputs=gr.Textbox(placeholder="Share your thoughts on COVID vaccines..."),
|
54 |
+
outputs="label",
|
55 |
+
interpretation="default",
|
56 |
+
examples=[
|
57 |
+
["I feel confident about covid vaccines"],
|
58 |
+
["I do not like the covid vaccine"],
|
59 |
+
["I like the covid vaccines"],
|
60 |
+
["The covid vaccines are effective"]
|
61 |
+
],
|
62 |
+
title="COVID Vaccine Sentiment Analysis",
|
63 |
+
description="An AI model that predicts sentiment about COVID vaccines, providing labels and probabilities for 'NEGATIVE', 'NEUTRAL', and 'POSITIVE' sentiments.",
|
64 |
+
theme="default",
|
65 |
+
live=True
|
66 |
+
)
|
67 |
+
|
68 |
+
if __name__ == "__main__":
|
69 |
+
demo.launch("0.0.0.0:7860")
|
notebook/Fine-tuning Hugging face text classification model - _distilbert-base-uncased_.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
notebook/Fine-tuning Hugging face text classification model - roberta-base.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|