AliArshad commited on
Commit
ccce6c3
·
verified ·
1 Parent(s): efbf5d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -34
app.py CHANGED
@@ -1,49 +1,42 @@
1
- import requests
2
- import torch
3
- from transformers import XLNetTokenizer, XLNetForSequenceClassification
4
  import gradio as gr
5
 
6
- # URL of the saved model on GitHub
7
- model_url = 'https://github.com/AliArshadswl/severity_prediction/raw/main/XLNet_model_project_Core.pt'
 
 
8
 
9
- # Function to download the model from URL and load it
10
- def download_model(url):
11
- response = requests.get(url)
12
- with open('XLNet_model_project_Core.pt', 'wb') as f:
13
- f.write(response.content)
14
 
15
- # Download the model
16
- download_model(model_url)
 
 
17
 
18
- # Load the saved model
19
- tokenizer = XLNetTokenizer.from_pretrained('xlnet-base-cased')
20
- model = XLNetForSequenceClassification.from_pretrained('xlnet-base-cased', num_labels=2)
21
- model.load_state_dict(torch.load('XLNet_model_project_Core.pt', map_location=torch.device('cpu')))
22
- model.eval()
23
 
24
- # Function for prediction
25
- def xl_net_predict(text):
26
- inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=100)
27
- with torch.no_grad():
28
- outputs = model(**inputs)
29
- logits = outputs.logits
30
- probabilities = torch.softmax(logits, dim=1)
31
- predicted_class = torch.argmax(probabilities).item()
32
- return "Severe" if predicted_class == 1 else "Non-severe"
33
 
34
- # Customizing the interface
35
  iface = gr.Interface(
36
- fn=xl_net_predict,
37
- inputs=gr.Textbox(lines=2, label="Summary", placeholder="Enter text here..."),
38
- outputs=gr.Textbox(label="Predicted Severity"),
39
- title="SevPrecit - A GPT-2 Based Bug Report Severity Prediction",
 
 
 
40
  description="Enter text and predict its severity (Severe or Non-severe).",
41
- theme="huggingface",
42
  examples=[
43
  ["Can't open multiple bookmarks at once from the bookmarks sidebar using the context menu"],
44
  ["Minor enhancements to make-source-package.sh"]
45
- ],
46
- allow_flagging=False
47
  )
48
 
 
49
  iface.launch()
 
1
+ from transformers import pipeline
 
 
2
  import gradio as gr
3
 
4
+ # Load the model using the pipeline
5
+ pipe = pipeline("text-classification", model="AliArshad/Severity_Predictor")
6
+ from transformers import pipeline
7
+ import gradio as gr
8
 
9
+ # Load the model using the pipeline
10
+ pipe = pipeline("text-classification", model="AliArshad/Severity_Predictor")
 
 
 
11
 
12
+ # Function to predict severity and return confidence score
13
+ def predict_severity(text):
14
+ # Get prediction from the pipeline
15
+ prediction = pipe(text)
16
 
17
+ # Interpret the label and get the confidence score
18
+ label = prediction[0]['label']
19
+ confidence = prediction[0]['score']
20
+ severity = "Severe" if label == "LABEL_1" else "Non-Severe"
 
21
 
22
+ # Return severity and confidence as separate outputs
23
+ return severity, confidence
 
 
 
 
 
 
 
24
 
25
+ # Define the Gradio interface with a title, specific placeholder message, and a progress bar for confidence
26
  iface = gr.Interface(
27
+ fn=predict_severity,
28
+ inputs=gr.Textbox(lines=2, placeholder="Please Enter Bug Report Summary"),
29
+ outputs=[
30
+ gr.Textbox(label="Prediction"),
31
+ gr.Number(label="Confidence", precision=2)
32
+ ],
33
+ title="SevPredict: GPT-2 Based Severity Prediction",
34
  description="Enter text and predict its severity (Severe or Non-severe).",
 
35
  examples=[
36
  ["Can't open multiple bookmarks at once from the bookmarks sidebar using the context menu"],
37
  ["Minor enhancements to make-source-package.sh"]
38
+ ]
 
39
  )
40
 
41
+ # Launch the interface
42
  iface.launch()