ManjinderUNCC commited on
Commit
5b443d4
·
verified ·
1 Parent(s): a34a694

Create gradio_interface.py

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