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

updated api

Browse files
app/__init__.py ADDED
File without changes
app/core/__init__.py ADDED
File without changes
app/core/security.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from fastapi import Header, HTTPException
2
+
3
+ API_KEY = "12345"
4
+
5
+ def verify_api_key(x_api_key: str = Header(...)):
6
+ if x_api_key != API_KEY:
7
+ raise HTTPException(status_code=401, detail="Unauthorized")
app/main.py CHANGED
@@ -1,75 +1,13 @@
1
- from fastapi import FastAPI, Body
2
- from pydantic import BaseModel
3
- from app import models, prompts
4
- import logging
5
-
6
- # Setup basic logging
7
- logging.basicConfig(level=logging.INFO)
8
- logger = logging.getLogger(__name__)
9
 
10
  app = FastAPI()
11
 
12
- class TextInput(BaseModel):
13
- text: str
14
- mode: str
15
- tone: str = None
16
- target_lang: str = None
17
-
18
  @app.get("/")
19
  def root():
20
- return {"message": "Welcome to Build Your Own Grammarly API. Use /rewrite POST endpoint."}
21
-
22
- @app.post("/rewrite")
23
- def rewrite(input: TextInput):
24
- text = input.text
25
- mode = input.mode
26
- logger.info(f"Received request | Mode: {mode} | Text: {text}")
27
-
28
- try:
29
- if mode == "grammar":
30
- result = models.run_grammar_correction(text)
31
- logger.info(f"Grammar corrected: {result}")
32
- return {"result": result}
33
-
34
- elif mode == "paraphrase":
35
- prompt = prompts.paraphrase_prompt(text)
36
- result = models.run_flan_prompt(prompt)
37
- logger.info(f"Paraphrased: {result}")
38
- return {"result": result}
39
-
40
- elif mode == "clarity":
41
- prompt = prompts.clarity_prompt(text)
42
- result = models.run_flan_prompt(prompt)
43
- logger.info(f"Clarity enhanced: {result}")
44
- return {"result": result}
45
-
46
- elif mode == "fluency":
47
- prompt = prompts.fluency_prompt(text)
48
- result = models.run_flan_prompt(prompt)
49
- logger.info(f"Fluency improved: {result}")
50
- return {"result": result}
51
-
52
- elif mode == "tone" and input.tone:
53
- prompt = prompts.tone_prompt(text, input.tone)
54
- result = models.run_flan_prompt(prompt)
55
- logger.info(f"Tone changed to {input.tone}: {result}")
56
- return {"result": result}
57
-
58
- elif mode == "translate" and input.target_lang:
59
- result = models.run_translation(text, input.target_lang)
60
- logger.info(f"Translated to {input.target_lang}: {result}")
61
- return {"result": result}
62
-
63
- elif mode == "pronoun":
64
- prompt = prompts.pronoun_friendly_prompt(text)
65
- result = models.run_flan_prompt(prompt)
66
- logger.info(f"Inclusive pronoun version: {result}")
67
- return {"result": result}
68
-
69
- else:
70
- logger.error("Invalid request parameters.")
71
- return {"error": "Invalid request"}
72
 
73
- except Exception as e:
74
- logger.error(f"Error while processing: {str(e)}")
75
- return {"error": str(e)}
 
 
1
+ from fastapi import FastAPI
2
+ from app.routers import analyze, paraphrase, translate, summarize
 
 
 
 
 
 
3
 
4
  app = FastAPI()
5
 
 
 
 
 
 
 
6
  @app.get("/")
7
  def root():
8
+ return {"message": "Welcome to Grammafree API"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ app.include_router(analyze.router)
11
+ app.include_router(paraphrase.router)
12
+ app.include_router(translate.router)
13
+ app.include_router(summarize.router)
app/prompts.py CHANGED
@@ -10,5 +10,14 @@ def fluency_prompt(text):
10
  def paraphrase_prompt(text):
11
  return f"Paraphrase: {text}"
12
 
 
 
 
13
  def pronoun_friendly_prompt(text):
14
  return f"Rewrite the text using inclusive and non-offensive pronouns: {text}"
 
 
 
 
 
 
 
10
  def paraphrase_prompt(text):
11
  return f"Paraphrase: {text}"
12
 
13
+ def summarize_prompt(text):
14
+ return f"Summarize: {text}"
15
+
16
  def pronoun_friendly_prompt(text):
17
  return f"Rewrite the text using inclusive and non-offensive pronouns: {text}"
18
+
19
+ def active_voice_prompt(text):
20
+ return f"Detect if this is passive or active voice. If passive, suggest an active voice version: {text}"
21
+
22
+ def tone_analysis_prompt(text):
23
+ return f"Analyze the tone of the following text and suggest improvements if needed: {text}"
app/routers/__init__.py ADDED
File without changes
app/routers/analyze.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }
app/routers/paraphrase.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Input(BaseModel):
9
+ text: str
10
+
11
+ @router.post("/paraphrase", dependencies=[Depends(verify_api_key)])
12
+ def paraphrase(input: Input):
13
+ return {"result": models.run_flan_prompt(prompts.paraphrase_prompt(input.text))}
app/routers/summarize.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Input(BaseModel):
9
+ text: str
10
+
11
+ @router.post("/summarize", dependencies=[Depends(verify_api_key)])
12
+ def summarize(input: Input):
13
+ return {"result": models.run_flan_prompt(prompts.summarize_prompt(input.text))}
app/routers/translate.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Depends
2
+ from pydantic import BaseModel
3
+ from app import models
4
+ from app.core.security import verify_api_key
5
+
6
+ router = APIRouter()
7
+
8
+ class TranslateInput(BaseModel):
9
+ text: str
10
+ target_lang: str
11
+
12
+ @router.post("/translate", dependencies=[Depends(verify_api_key)])
13
+ def translate(input: TranslateInput):
14
+ return {"result": models.run_translation(input.text, input.target_lang)}