ittyavira commited on
Commit
ac41f6e
ยท
verified ยท
1 Parent(s): 8d04fab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -23
app.py CHANGED
@@ -1,41 +1,79 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load grammar correction model
5
- grammar_corrector = pipeline("text2text-generation", model="vennify/t5-base-grammar-correction")
 
6
 
7
- # Load text generation model for meaning & phrase suggestion
8
- meaning_generator = pipeline("text2text-generation", model="facebook/bart-large-cnn")
9
-
10
- def analyze_text(user_input):
11
  try:
12
- # Grammar correction
13
- corrected = grammar_corrector(user_input, max_length=128, clean_up_tokenization_spaces=True)[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # Meaning / Usage generation
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
- # Phrase suggestion
20
- phrase_prompt = f"Suggest a few grammatically correct alternative phrases for: {user_input}"
21
- phrases = meaning_generator(phrase_prompt, max_length=100, clean_up_tokenization_spaces=True)[0]['generated_text']
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=analyze_text,
35
- inputs=gr.Textbox(label="Enter a word, phrase, or sentence", placeholder="Type here..."),
 
 
 
36
  outputs=gr.Markdown(label="AI Response"),
37
  title="Dictionary & Grammar Chatbot",
38
- description="AI-powered chatbot that provides meanings, grammar corrections, and phrase suggestions.",
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