ManjinderUNCC commited on
Commit
adda257
·
verified ·
1 Parent(s): 21b1d28

Upload gradio_interface.py

Browse files
Files changed (1) hide show
  1. gradio_interface.py +43 -0
gradio_interface.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Import necessary libraries
2
+ import gradio as gr
3
+ import spacy
4
+
5
+ # Load the trained spaCy model
6
+ model_path = "./my_trained_model"
7
+ nlp = spacy.load(model_path)
8
+
9
+ # Function to classify text
10
+ def classify_text(text):
11
+ doc = nlp(text)
12
+ predicted_labels = doc.cats
13
+ return predicted_labels
14
+
15
+ # Function to save results to a file
16
+ def save_to_file(text, predicted_labels):
17
+ with open("classification_results.txt", "w") as f:
18
+ f.write("Text: {}\n\n".format(text))
19
+ for label, score in predicted_labels.items():
20
+ f.write("{}: {}\n".format(label, score))
21
+
22
+ # Gradio Interface
23
+ inputs = [
24
+ gr.inputs.Textbox(lines=7, label="Enter your text"),
25
+ gr.inputs.File(label="Upload a file")
26
+ ]
27
+
28
+ output = gr.outputs.Textbox(label="Classification Results")
29
+
30
+ def classify_and_save(input_text, input_file):
31
+ if input_text:
32
+ text = input_text
33
+ elif input_file:
34
+ # Process the file and extract text
35
+ with open(input_file.name, "r") as f:
36
+ text = f.read()
37
+
38
+ predicted_labels = classify_text(text)
39
+ save_to_file(text, predicted_labels)
40
+ return predicted_labels
41
+
42
+ iface = gr.Interface(fn=classify_and_save, inputs=inputs, outputs=output, title="Text Classifier")
43
+ iface.launch(share=True)