Oscar Wang commited on
Commit
ca12785
·
verified ·
1 Parent(s): e1e3a6c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -1,11 +1,39 @@
1
- import os
2
  import gradio as gr
 
 
3
 
4
- # Load the Hugging Face access token from environment variables
5
- hf_token = os.getenv("HUGGING_FACE_TOKEN")
 
 
6
 
7
- if hf_token is None:
8
- raise ValueError("Hugging Face access token not found in environment variables.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Load the model using the access token
11
- gr.load("GoalZero/GoalZero-latest", hf_token=hf_token).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import RobertaTokenizer, RobertaForSequenceClassification
3
+ import torch
4
 
5
+ # Load the model and tokenizer from the specified directory
6
+ model_path = './finetuned_roberta'
7
+ tokenizer = RobertaTokenizer.from_pretrained(model_path)
8
+ model = RobertaForSequenceClassification.from_pretrained(model_path)
9
 
10
+ # Define the prediction function
11
+ def classify_text(text):
12
+ # Tokenize the input text
13
+ inputs = tokenizer(text, return_tensors='pt', padding=True, truncation=True, max_length=128)
14
+
15
+ # Get the model's prediction
16
+ with torch.no_grad():
17
+ outputs = model(**inputs)
18
+
19
+ # Apply softmax to get probabilities
20
+ probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
21
+
22
+ # Get the probability of the class '1'
23
+ prob_1 = probabilities[0][1].item()
24
+
25
+ return {"Probability of being 1": prob_1}
26
 
27
+ # Create the Gradio interface
28
+ iface = gr.Interface(
29
+ fn=classify_text,
30
+ inputs=gr.Textbox(lines=2, placeholder="Enter text here..."),
31
+ outputs="json",
32
+ title="Text Classification with RoBERTa",
33
+ description="Enter some text and get the probability of the text being classified as class 1.",
34
+ enable_queue=True, # Ensure the API is enabled
35
+ )
36
+
37
+ # Launch the app
38
+ if __name__ == "__main__":
39
+ iface.launch()