Skip to content

Commit 62c1e4e

Browse files
Merge pull request #1795 from yashhibare7/yash
Language Detection
2 parents 2c39331 + 92dd34a commit 62c1e4e

File tree

9 files changed

+11431
-0
lines changed

9 files changed

+11431
-0
lines changed

Language Detection/Language Detection.ipynb

Lines changed: 497 additions & 0 deletions
Large diffs are not rendered by default.

Language Detection/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# pblapi.github.io

Language Detection/app.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# importing libraries
2+
from flask import Flask, request, render_template
3+
import sklearn
4+
import pickle
5+
import pandas as pd
6+
import re
7+
import numpy as np
8+
from sklearn.feature_extraction.text import CountVectorizer
9+
from sklearn.naive_bayes import MultinomialNB
10+
from sklearn.preprocessing import LabelEncoder
11+
from googletrans import Translator
12+
le = LabelEncoder()
13+
14+
app = Flask(__name__)
15+
@app.route('/')
16+
17+
def home():
18+
return render_template("home.html")
19+
20+
@app.route("/predict", methods = ["POST"])
21+
22+
def predict():
23+
# loading the dataset
24+
data = pd.read_csv("language_detection.csv")
25+
y = data["Language"]
26+
27+
# label encoding
28+
y = le.fit_transform(y)
29+
30+
#loading the model and cv
31+
model = pickle.load(open("model.pkl", "rb"))
32+
cv = pickle.load(open("transform.pkl", "rb"))
33+
34+
if request.method == "POST":
35+
# taking the input
36+
text = request.form["text"]
37+
# preprocessing the text
38+
text = re.sub(r'[!@#$(),\n"%^*?\:;~`0-9]', '', text)
39+
text = re.sub(r'[[]]', '', text)
40+
text = text.lower()
41+
dat = [text]
42+
# creating the vector
43+
vect = cv.transform(dat).toarray()
44+
# prediction
45+
my_pred = model.predict(vect)
46+
my_pred = le.inverse_transform(my_pred)
47+
48+
return render_template("home.html", pred="{}".format(my_pred[0]))
49+
@app.route("/translate", methods=["POST"])
50+
def translate():
51+
translator = Translator()
52+
if request.method == "POST":
53+
# taking the input
54+
text = request.form["text"]
55+
# taking the target language
56+
target_lang = request.form["lang"]
57+
# translating the text
58+
translation = translator.translate(text, dest=target_lang)
59+
return render_template("home.html", translation=translation.text)
60+
61+
if __name__ =="__main__":
62+
app.run(debug=True)

Language Detection/language_detection.csv

Lines changed: 10781 additions & 0 deletions
Large diffs are not rendered by default.

Language Detection/model.pkl

10.2 MB
Binary file not shown.

Language Detection/requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Flask==1.1.2
2+
scikit-learn==0.23.1
3+
numpy==1.18.5
4+
pandas==1.1.4
5+
regex==2020.11.13
6+
Werkzeug==1.0.1
7+
gunicorn

Language Detection/static/style.css

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
font-size: 16px;
4+
}
5+
6+
h1 {
7+
text-align: center;
8+
}
9+
10+
form {
11+
margin: 20px auto;
12+
max-width: 500px;
13+
}
14+
15+
label {
16+
display: block;
17+
margin-bottom: 5px;
18+
}
19+
20+
textarea {
21+
display: block;
22+
width: 100%;
23+
padding: 10px;
24+
margin-bottom: 10px;
25+
border: 1px solid #ccc;
26+
border-radius: 3px;
27+
box-sizing: border-box;
28+
resize: vertical;
29+
}
30+
31+
select {
32+
display: block;
33+
padding: 5px;
34+
margin-bottom: 10px;
35+
border: 1px solid #ccc;
36+
border-radius: 3px;
37+
box-sizing: border-box;
38+
}
39+
40+
button[type="submit"] {
41+
display: block;
42+
padding: 10px 20px;
43+
background-color: #4CAF50;
44+
color: white;
45+
border: none;
46+
border-radius: 3px;
47+
cursor: pointer;
48+
}
49+
50+
button[type="submit"]:hover {
51+
background-color: #3e8e41;
52+
}
53+
54+
#result, #translation_result {
55+
margin: 20px auto;
56+
max-width: 500px;
57+
padding: 10px;
58+
border: 1px solid #ccc;
59+
border-radius: 3px;
60+
background-color: #f2f2f2;
61+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Language Detection</title>
6+
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
7+
</head>
8+
<body>
9+
<h1>Language Detection</h1>
10+
<form id="language-detection" action="/predict" method="post">
11+
<label for="text">Enter text:</label>
12+
<textarea name="text" id="text" rows="5" cols="30"></textarea>
13+
<br>
14+
<button type="submit" name="action" value="detect">Detect language</button>
15+
</form>
16+
{% if pred %}
17+
<div id="result">
18+
<p>The entered text is in {{ pred }}</p>
19+
</div>
20+
{% endif %}
21+
</body>
22+
</html>

Language Detection/transform.pkl

1.74 MB
Binary file not shown.

0 commit comments

Comments
 (0)