iamspruce commited on
Commit
3e24d97
·
1 Parent(s): 22c7fb1

updated the api

Browse files
Files changed (3) hide show
  1. app/models.py +9 -1
  2. app/routers/analyze.py +42 -10
  3. requirements.txt +2 -0
app/models.py CHANGED
@@ -1,4 +1,4 @@
1
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
2
  import torch
3
 
4
  device = torch.device("cpu")
@@ -29,3 +29,11 @@ def run_translation(text: str, target_lang: str):
29
  inputs = trans_tokenizer(f">>{target_lang}<< {text}", return_tensors="pt").to(device)
30
  outputs = trans_model.generate(**inputs)
31
  return trans_tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, pipeline
2
  import torch
3
 
4
  device = torch.device("cpu")
 
29
  inputs = trans_tokenizer(f">>{target_lang}<< {text}", return_tensors="pt").to(device)
30
  outputs = trans_model.generate(**inputs)
31
  return trans_tokenizer.decode(outputs[0], skip_special_tokens=True)
32
+
33
+
34
+ # Add this at the bottom of models.py
35
+ tone_classifier = pipeline("text-classification", model="bhadresh-savani/bert-base-uncased-emotion", top_k=1)
36
+
37
+ def classify_tone(text: str):
38
+ result = tone_classifier(text)[0][0]
39
+ return result['label']
app/routers/analyze.py CHANGED
@@ -1,21 +1,53 @@
1
- from fastapi import APIRouter, Depends
2
  from pydantic import BaseModel
3
  from app import models, prompts
4
  from app.core.security import verify_api_key
 
 
5
 
6
  router = APIRouter()
 
 
7
 
8
  class AnalyzeInput(BaseModel):
9
  text: str
10
 
11
- @router.post("/analyze", dependencies=[Depends(verify_api_key)])
12
- def analyze_text(input: AnalyzeInput):
13
- text = input.text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  return {
15
- "grammar": models.run_grammar_correction(text),
16
- "punctuation": models.run_flan_prompt(prompts.clarity_prompt(text)),
17
- "sentence_correctness": models.run_flan_prompt(prompts.fluency_prompt(text)),
18
- "tone_analysis": models.run_flan_prompt(prompts.tone_analysis_prompt(text)),
19
- "voice": models.run_flan_prompt(prompts.active_voice_prompt(text)),
20
- "inclusive_pronouns": models.run_flan_prompt(prompts.pronoun_friendly_prompt(text))
 
 
21
  }
 
1
+ from fastapi import APIRouter, Depends, Request
2
  from pydantic import BaseModel
3
  from app import models, prompts
4
  from app.core.security import verify_api_key
5
+ import language_tool_python
6
+ import spacy
7
 
8
  router = APIRouter()
9
+ nlp = spacy.load("en_core_web_sm")
10
+ tool = language_tool_python.LanguageTool('en-US')
11
 
12
  class AnalyzeInput(BaseModel):
13
  text: str
14
 
15
+ @router.post("/analyze")
16
+ def analyze_text(payload: AnalyzeInput, request: Request = Depends(verify_api_key)):
17
+ text = payload.text
18
+
19
+ # 1. Grammar Correction
20
+ grammar = models.run_grammar_correction(text)
21
+
22
+ # 2. Punctuation Fixes
23
+ matches = tool.check(text)
24
+ punctuation_fixes = [m.message for m in matches if 'PUNCTUATION' in m.ruleId.upper()]
25
+
26
+ # 3. Sentence Correctness Tips
27
+ sentence_issues = [m.message for m in matches if 'PUNCTUATION' not in m.ruleId.upper()]
28
+
29
+ # 4. Tone Detection
30
+ tone_result = models.classify_tone(text)
31
+ better_tone_version = models.run_flan_prompt(prompts.tone_prompt(text, "formal"))
32
+
33
+ # 5. Active/Passive Voice
34
+ doc = nlp(text)
35
+ voice = "passive" if any(tok.dep_ == "auxpass" for tok in doc) else "active"
36
+ if voice == "passive":
37
+ better_voice = models.run_flan_prompt(f"Rewrite this in active voice: {text}")
38
+ else:
39
+ better_voice = "Already in active voice"
40
+
41
+ # 6. Inclusive Pronoun Suggestion
42
+ inclusive = models.run_flan_prompt(prompts.pronoun_friendly_prompt(text))
43
+
44
  return {
45
+ "grammar": grammar,
46
+ "punctuation_fixes": punctuation_fixes,
47
+ "sentence_issues": sentence_issues,
48
+ "tone": tone_result,
49
+ "tone_suggestion": better_tone_version,
50
+ "voice": voice,
51
+ "voice_suggestion": better_voice,
52
+ "inclusive_pronouns": inclusive
53
  }
requirements.txt CHANGED
@@ -6,3 +6,5 @@ sentencepiece
6
  pyspellchecker
7
  spacy
8
  nltk
 
 
 
6
  pyspellchecker
7
  spacy
8
  nltk
9
+ language-tool-python
10
+ scikit-learn