File size: 1,871 Bytes
e6eef41
 
 
2a690cd
e6eef41
 
c4dc6bd
e6eef41
956bcba
9d68039
 
 
df6ff7d
9d68039
 
 
 
 
 
 
 
 
df6ff7d
9d68039
 
df6ff7d
 
9d68039
 
 
 
 
 
 
 
 
 
 
e6eef41
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import gradio as gr
import pandas as pd
import difflib  # For smart suggestions

# Load your CSV data
df = pd.read_csv("Babeli.csv")

# Function to translate using your CSV data
def translate_text(text):
    # Clean the user's input: remove spaces, punctuation, and make it lowercase
    def clean_phrase(phrase):
        return phrase.strip().rstrip('?.!').lower()
    
    user_input_clean = clean_phrase(text)
    
    # Create a list of all cleaned phrases from our CSV for matching
    all_phrases_in_csv = []
    for index, row in df.iterrows():
        csv_phrase = str(row['Phrase in your language'])
        all_phrases_in_csv.append(clean_phrase(csv_phrase))
    
    # 1. First, try to find an exact match
    for index, row in df.iterrows():
        csv_phrase_clean = clean_phrase(str(row['Phrase in your language']))
        if user_input_clean == csv_phrase_clean:
            return row['English translation']
    
    # 2. If no exact match, find the closest suggestion
    suggestions = difflib.get_close_matches(user_input_clean, all_phrases_in_csv, n=1, cutoff=0.6)
    
    if suggestions:
        closest_match = suggestions[0]
        # Find the English translation for the closest match
        for index, row in df.iterrows():
            if clean_phrase(str(row['Phrase in your language'])) == closest_match:
                return f"Did you mean: '{row['Phrase in your language']}'? Translation: {row['English translation']}"
    
    # 3. If nothing is close enough
    return "Translation not found. Try 'Kita kobor?' or 'Kita korde?'."

# Create the app interface
demo = gr.Interface(
    fn=translate_text,
    inputs=gr.Textbox(label="Type phrase in our language"),
    outputs=gr.Textbox(label="Translation"),
    title="Our Language Translator",
    description="This app uses our custom language data."
)

# Launch the app
demo.launch()