Delete app.py
Browse files12345678910111213141516171819202122232425262728293031323334import jsonimport gradio as gr
def load_ngrams_jsonl(file_name): dictionary = {} with open(file_name, encoding='utf-8') as f: for line in f: data = json.loads(line) k = tuple(data["ngram"]) if len(data["ngram"]) > 1 else data["ngram"][0] dictionary[k] = data["count"] return dictionary
frequency_trigrams = load_ngrams_jsonl('trigrams.jsonl')
def predict_next_word(text): words = text.lower().split() if len(words) < 2: return "Escribe al menos dos palabras en sueco (p. ej. 'det var')." w1, w2 = words[-2], words[-1] candidates = [(freq, w3) for (a, b, w3), freq in frequency_trigrams.items() if a == w1 and b == w2] sorted_candidates = sorted(candidates, key=lambda x: (-x[0], x[1])) top5 = [w for freq, w in sorted_candidates[:5]] return ", ".join(top5) if top5 else "No hay predicciones para ese contexto."
demo = gr.Interface( fn=predict_next_word, inputs=gr.Textbox(label="Escribe al menos dos palabras en sueco", placeholder="det var"), outputs=gr.Textbox(label="Top-5 predicciones de la siguiente palabra"), title="Selma Lagerlöf — Predicción de palabra (modelo de trigramas)", description="Basado en n-gramas contados sobre el corpus de Selma Lagerlöf (Assignment 2, Language Technology, Lund University).")
demo.launch()app.pyDeleted-34