Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,25 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
import difflib
|
4 |
|
5 |
-
# Load your CSV data
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
# Function to translate using your CSV data
|
9 |
def translate_text(text):
|
10 |
if not text.strip():
|
11 |
return "Please enter a phrase or question"
|
12 |
|
|
|
|
|
|
|
|
|
13 |
# Normalize text
|
14 |
def normalize_phrase(phrase):
|
15 |
return ''.join(char for char in phrase.lower() if char.isalpha() or char.isspace())
|
@@ -19,9 +30,17 @@ def translate_text(text):
|
|
19 |
# Get all phrases from CSV for matching
|
20 |
all_phrases = []
|
21 |
for index, row in df.iterrows():
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# 1. First try exact match
|
27 |
for clean_csv, translation, meaning, original in all_phrases:
|
@@ -33,7 +52,7 @@ def translate_text(text):
|
|
33 |
user_clean,
|
34 |
[clean_csv for clean_csv, trans, meaning, orig in all_phrases],
|
35 |
n=3,
|
36 |
-
cutoff=0.6
|
37 |
)
|
38 |
|
39 |
if close_matches:
|
@@ -42,55 +61,28 @@ def translate_text(text):
|
|
42 |
for clean_csv, translation, meaning, original in all_phrases:
|
43 |
if clean_csv == match:
|
44 |
similarity = int(difflib.SequenceMatcher(None, user_clean, clean_csv).ratio() * 100)
|
45 |
-
if similarity >= 60:
|
46 |
suggestions.append(f"π― '{original}' ({similarity}% match)\nπ¬ {translation}\nπ {meaning}")
|
47 |
|
48 |
if suggestions:
|
49 |
return "π SIMILAR PHRASES FOUND:\n\n" + "\n\n".join(suggestions)
|
50 |
|
51 |
-
# 3.
|
52 |
-
partial_matches = []
|
53 |
-
for clean_csv, translation, meaning, original in all_phrases:
|
54 |
-
if user_clean in clean_csv or clean_csv in user_clean:
|
55 |
-
similarity = int(difflib.SequenceMatcher(None, user_clean, clean_csv).ratio() * 100)
|
56 |
-
partial_matches.append((original, translation, meaning, similarity))
|
57 |
-
|
58 |
-
if partial_matches:
|
59 |
-
# Sort by similarity score
|
60 |
-
partial_matches.sort(key=lambda x: x[3], reverse=True)
|
61 |
-
best_match = partial_matches[0]
|
62 |
-
return f"π PARTIAL MATCH:\nπ¬ '{best_match[0]}' -> {best_match[1]}\nπ {best_match[2]}\nβ‘ {best_match[3]}% similar"
|
63 |
-
|
64 |
-
# 4. Very loose matching for completely different questions
|
65 |
-
loose_matches = difflib.get_close_matches(
|
66 |
-
user_clean,
|
67 |
-
[clean_csv for clean_csv, trans, meaning, orig in all_phrases],
|
68 |
-
n=2,
|
69 |
-
cutoff=0.3 # Very low threshold
|
70 |
-
)
|
71 |
-
|
72 |
-
if loose_matches:
|
73 |
-
return "β COMPLETELY DIFFERENT QUESTION\n\nπ Try asking about:\n" + \
|
74 |
-
"\n".join([f"β’ '{orig}'" for clean_csv, trans, meaning, orig in all_phrases
|
75 |
-
if clean_csv in loose_matches][:3]) + \
|
76 |
-
"\n\nπ‘ Or ask something related to daily conversations"
|
77 |
-
|
78 |
-
# 5. Final fallback - show sample phrases
|
79 |
sample_phrases = [orig for clean_csv, trans, meaning, orig in all_phrases[:5]]
|
80 |
return "β ASK DIFFERENT QUESTION\n\n" + \
|
81 |
-
"π
|
82 |
"\n".join([f"β’ '{phrase}'" for phrase in sample_phrases]) + \
|
83 |
-
"\n\nπ‘
|
84 |
|
85 |
# Create the app interface
|
86 |
demo = gr.Interface(
|
87 |
fn=translate_text,
|
88 |
inputs=gr.Textbox(label="Type phrase in our language",
|
89 |
-
placeholder="Example:
|
90 |
outputs=gr.Textbox(label="Translation Result"),
|
91 |
title="π Smart Dialect Translator",
|
92 |
-
description="Translates with smart matching - finds similar phrases
|
93 |
-
examples=[["gesle ni"], ["Kita kobor?"], ["Goto kali"]
|
94 |
)
|
95 |
|
96 |
# Launch the app
|
|
|
2 |
import pandas as pd
|
3 |
import difflib
|
4 |
|
5 |
+
# Load your CSV data with error handling
|
6 |
+
try:
|
7 |
+
df = pd.read_csv("dialect_data.csv")
|
8 |
+
print("CSV loaded successfully!")
|
9 |
+
print("Available columns:", df.columns.tolist())
|
10 |
+
except Exception as e:
|
11 |
+
print(f"Error loading CSV: {e}")
|
12 |
+
# Create empty dataframe as fallback
|
13 |
+
df = pd.DataFrame(columns=['Dialect Bengali', 'Translation in Ac English', 'Meaning'])
|
14 |
|
15 |
# Function to translate using your CSV data
|
16 |
def translate_text(text):
|
17 |
if not text.strip():
|
18 |
return "Please enter a phrase or question"
|
19 |
|
20 |
+
# Check if dataframe is empty
|
21 |
+
if df.empty:
|
22 |
+
return "Database not loaded. Please check your CSV file."
|
23 |
+
|
24 |
# Normalize text
|
25 |
def normalize_phrase(phrase):
|
26 |
return ''.join(char for char in phrase.lower() if char.isalpha() or char.isspace())
|
|
|
30 |
# Get all phrases from CSV for matching
|
31 |
all_phrases = []
|
32 |
for index, row in df.iterrows():
|
33 |
+
try:
|
34 |
+
csv_phrase = str(row['Dialect Bengali'])
|
35 |
+
clean_csv = normalize_phrase(csv_phrase)
|
36 |
+
translation = str(row['Translation in Ac English'])
|
37 |
+
meaning = str(row['Meaning'])
|
38 |
+
all_phrases.append((clean_csv, translation, meaning, csv_phrase))
|
39 |
+
except:
|
40 |
+
continue
|
41 |
+
|
42 |
+
if not all_phrases:
|
43 |
+
return "No phrases found in database. Check CSV format."
|
44 |
|
45 |
# 1. First try exact match
|
46 |
for clean_csv, translation, meaning, original in all_phrases:
|
|
|
52 |
user_clean,
|
53 |
[clean_csv for clean_csv, trans, meaning, orig in all_phrases],
|
54 |
n=3,
|
55 |
+
cutoff=0.6
|
56 |
)
|
57 |
|
58 |
if close_matches:
|
|
|
61 |
for clean_csv, translation, meaning, original in all_phrases:
|
62 |
if clean_csv == match:
|
63 |
similarity = int(difflib.SequenceMatcher(None, user_clean, clean_csv).ratio() * 100)
|
64 |
+
if similarity >= 60:
|
65 |
suggestions.append(f"π― '{original}' ({similarity}% match)\nπ¬ {translation}\nπ {meaning}")
|
66 |
|
67 |
if suggestions:
|
68 |
return "π SIMILAR PHRASES FOUND:\n\n" + "\n\n".join(suggestions)
|
69 |
|
70 |
+
# 3. Show sample available phrases
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
sample_phrases = [orig for clean_csv, trans, meaning, orig in all_phrases[:5]]
|
72 |
return "β ASK DIFFERENT QUESTION\n\n" + \
|
73 |
+
"π Try these phrases:\n" + \
|
74 |
"\n".join([f"β’ '{phrase}'" for phrase in sample_phrases]) + \
|
75 |
+
"\n\nπ‘ Example: 'Kita kobor?', 'Goto kali', 'gesle ni'"
|
76 |
|
77 |
# Create the app interface
|
78 |
demo = gr.Interface(
|
79 |
fn=translate_text,
|
80 |
inputs=gr.Textbox(label="Type phrase in our language",
|
81 |
+
placeholder="Example: Kita kobor?, Goto kali, gesle ni..."),
|
82 |
outputs=gr.Textbox(label="Translation Result"),
|
83 |
title="π Smart Dialect Translator",
|
84 |
+
description="Translates with smart matching - finds similar phrases",
|
85 |
+
examples=[["gesle ni"], ["Kita kobor?"], ["Goto kali"]]
|
86 |
)
|
87 |
|
88 |
# Launch the app
|