GREESHMA-1/Stack_Over_FLow
0
1import gradio as gr
2import pickle, re, nltk
3import numpy as np
4from nltk.corpus import stopwords
5from nltk.tokenize import word_tokenize
6from nltk.stem import PorterStemmer
7
8nltk.download('stopwords')
9nltk.download('punkt_tab')
10
11with open("model.pkl", "rb") as f: model = pickle.load(f)
12with open("tfidf.pkl", "rb") as f: tfidf = pickle.load(f)
13with open("mlb.pkl", "rb") as f: mlb = pickle.load(f)
14
15ps = PorterStemmer()
16stp = stopwords.words("english")
17stp.remove("not")
18
19def preprocess(text):
20 text = text.lower()
21 text = re.sub("<.*?>", " ", text)
22 text = re.sub("https?://\S+", " ", text)
23 text = re.sub("\d", " ", text)
24 text = re.sub('[!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~]', " ", text)
25 tokens = [ps.stem(w) for w in word_tokenize(text) if w not in stp]
26 return " ".join(tokens)
27
28def predict_tags(title, body):
29 try:
30 combined = preprocess(title) + " " + preprocess(body)
31 X = tfidf.transform([combined])
32 y_pred = model.predict(X)
33 tags = mlb.inverse_transform(y_pred)
34
35 if tags and tags[0]:
36 return ", ".join(tags[0])
37 else:
38 return "python, machine-learning, deep-learning"
39
40 except Exception as e:
41 return f"Error: {str(e)}"
42
43demo = gr.Interface(
44 fn=predict_tags,
45 inputs=[
46 gr.Textbox(label="Question Title"),
47 gr.Textbox(label="Question Body", lines=5)
48 ],
49 outputs=gr.Textbox(label="Predicted Tags"),
50 title="Stack Overflow Tag Predictor",
51 description="Enter a Stack Overflow question to get automatic tag predictions."
52)
53
54demo.launch()