CoolFace
Apppublic

freak360/language-detection

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py67 linesDownload Raw Back to root
1import nltk
2from nltk.corpus import stopwords
3from nltk.tokenize import word_tokenize
4from nltk.stem import WordNetLemmatizer
5from string import punctuation
6import streamlit as st
7import pickle
8
9nltk.download('punkt')
10nltk.download('stopwords')
11nltk.download('wordnet')
12model = pickle.load(open("model.pkl", "rb"))
13vectorizer = pickle.load(open("vectorizer.pkl", "rb"))
14
15st.write("# Language Detection System")
16
17inputt = st.text_area("Enter text here")
18
19def preprocess_text(text):
20    punc = list(punctuation)
21    stop = stopwords.words('english')
22    bad_tokens = punc + stop
23    lemma = WordNetLemmatizer()
24    tokens = word_tokenize(text)
25    word_tokens = [t for t in tokens if t.isalpha()]
26    clean_tokens = [lemma.lemmatize(t.lower()) for t in word_tokens if t not in bad_tokens]
27    return ' '.join(t for t in clean_tokens)
28
29if st.button("Detect Language"):
30    processed_text = preprocess_text(inputt)
31    vectorized = vectorizer.transform([processed_text]).toarray()
32    prediction = model.predict(vectorized)[0]
33
34    if prediction == 1:
35        st.header("English")
36    if prediction == 2:
37        st.header("Malayalam")
38    if prediction == 3:
39        st.header("Hindi")
40    if prediction == 4:
41        st.header("Tamil")
42    if prediction == 5:
43        st.header("Portuguese")
44    if prediction == 6:
45        st.header("French")
46    if prediction == 7:
47        st.header("Dutch")
48    if prediction == 8:
49        st.header("Spanish")
50    if prediction == 9:
51        st.header("Greek")
52    if prediction == 10:
53        st.header("Russian")
54    if prediction == 11:
55        st.header("Danish")
56    if prediction == 12:
57        st.header("Italian")
58    if prediction == 13:
59        st.header("Turkish")
60    if prediction == 14:
61        st.header("Swedish")
62    if prediction == 15:
63        st.header("Arabic")
64    if prediction == 16:
65        st.header("German")
66    if prediction == 17:
67        st.header("Kannada")