Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,79 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
meaning_generator = pipeline("text2text-generation", model="facebook/bart-large-cnn")
|
| 9 |
-
|
| 10 |
-
def analyze_text(user_input):
|
| 11 |
try:
|
| 12 |
-
# Grammar
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
meaning_prompt = f"Explain the meaning and usage of the phrase: {user_input}"
|
| 17 |
-
meaning = meaning_generator(meaning_prompt, max_length=150, clean_up_tokenization_spaces=True)[0]['generated_text']
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
-
return
|
| 24 |
-
f"๐ง **Grammar Corrected:** {corrected}\n\n"
|
| 25 |
-
f"๐ **Meaning & Usage:** {meaning}\n\n"
|
| 26 |
-
f"๐ฌ **Phrase Suggestions:** {phrases}"
|
| 27 |
-
)
|
| 28 |
|
| 29 |
except Exception as e:
|
| 30 |
return f"โ ๏ธ Error: {str(e)}"
|
| 31 |
|
| 32 |
-
# Gradio UI
|
| 33 |
iface = gr.Interface(
|
| 34 |
-
fn=
|
| 35 |
-
inputs=gr.Textbox(
|
|
|
|
|
|
|
|
|
|
| 36 |
outputs=gr.Markdown(label="AI Response"),
|
| 37 |
title="Dictionary & Grammar Chatbot",
|
| 38 |
-
description="AI-powered chatbot that provides meanings, grammar corrections,
|
| 39 |
theme="gradio/soft"
|
| 40 |
)
|
| 41 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load models
|
| 5 |
+
grammar_model = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
|
| 6 |
+
meaning_model = pipeline("text2text-generation", model="facebook/bart-large-cnn")
|
| 7 |
|
| 8 |
+
def dictionary_grammar_bot(user_input):
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
+
# --- Grammar Correction ---
|
| 11 |
+
grammar_output = grammar_model(
|
| 12 |
+
user_input,
|
| 13 |
+
max_new_tokens=128,
|
| 14 |
+
clean_up_tokenization_spaces=True
|
| 15 |
+
)[0]['generated_text']
|
| 16 |
+
|
| 17 |
+
# --- Meaning / Definition ---
|
| 18 |
+
meaning_prompt = f"Explain the meaning and definition of the word or phrase: {user_input}"
|
| 19 |
+
meaning_output = meaning_model(
|
| 20 |
+
meaning_prompt,
|
| 21 |
+
max_new_tokens=200,
|
| 22 |
+
clean_up_tokenization_spaces=True
|
| 23 |
+
)[0]['generated_text']
|
| 24 |
+
|
| 25 |
+
# --- Phrase Suggestions ---
|
| 26 |
+
phrase_prompt = f"Suggest grammatically correct alternative phrases or rephrasings for: {user_input}"
|
| 27 |
+
phrase_output = meaning_model(
|
| 28 |
+
phrase_prompt,
|
| 29 |
+
max_new_tokens=120,
|
| 30 |
+
clean_up_tokenization_spaces=True
|
| 31 |
+
)[0]['generated_text']
|
| 32 |
+
|
| 33 |
+
# --- Example Sentence ---
|
| 34 |
+
sentence_prompt = f"Write an example sentence using the phrase: {user_input}"
|
| 35 |
+
sentence_output = meaning_model(
|
| 36 |
+
sentence_prompt,
|
| 37 |
+
max_new_tokens=120,
|
| 38 |
+
clean_up_tokenization_spaces=True
|
| 39 |
+
)[0]['generated_text']
|
| 40 |
+
|
| 41 |
+
# Format structured output
|
| 42 |
+
response = f"""
|
| 43 |
+
### ๐งฉ Grammar Correction
|
| 44 |
+
{grammar_output}
|
| 45 |
+
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
### ๐ Meaning / Definition
|
| 49 |
+
{meaning_output}
|
| 50 |
+
|
| 51 |
+
---
|
| 52 |
+
|
| 53 |
+
### ๐ฌ Phrase Suggestions
|
| 54 |
+
{phrase_output}
|
| 55 |
|
| 56 |
+
---
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
### โ๏ธ Example Sentence
|
| 59 |
+
{sentence_output}
|
| 60 |
+
"""
|
| 61 |
|
| 62 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
return f"โ ๏ธ Error: {str(e)}"
|
| 66 |
|
| 67 |
+
# --- Gradio UI ---
|
| 68 |
iface = gr.Interface(
|
| 69 |
+
fn=dictionary_grammar_bot,
|
| 70 |
+
inputs=gr.Textbox(
|
| 71 |
+
label="Enter a word, phrase, or sentence",
|
| 72 |
+
placeholder="Example: resistance movement failed"
|
| 73 |
+
),
|
| 74 |
outputs=gr.Markdown(label="AI Response"),
|
| 75 |
title="Dictionary & Grammar Chatbot",
|
| 76 |
+
description="AI-powered chatbot that provides meanings, grammar corrections, phrase suggestions, and example sentences.",
|
| 77 |
theme="gradio/soft"
|
| 78 |
)
|
| 79 |
|