anisgtboi's picture
Update app.py
e6eef41 verified
raw
history blame
1.87 kB
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()