Spaces:
Runtime error
Runtime error
iamspruce
commited on
Commit
·
3e24d97
1
Parent(s):
22c7fb1
updated the api
Browse files- app/models.py +9 -1
- app/routers/analyze.py +42 -10
- 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"
|
| 12 |
-
def analyze_text(
|
| 13 |
-
text =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return {
|
| 15 |
-
"grammar":
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"
|
|
|
|
|
|
|
| 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
|