viskav commited on
Commit
6fbb945
·
verified ·
1 Parent(s): 8bccb6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -38
app.py CHANGED
@@ -5,70 +5,84 @@ from llama_cpp import Llama
5
  from contextlib import asynccontextmanager
6
  from huggingface_hub import hf_hub_download
7
 
8
-
9
- # =========================
10
- # MODEL CONFIG
11
- # =========================
12
  MODEL_REPO = "bartowski/Phi-3.1-mini-4k-instruct-GGUF"
13
  MODEL_FILE = "Phi-3.1-mini-4k-instruct-IQ2_M.gguf"
14
 
15
- MODEL_PATH = hf_hub_download(
16
- repo_id=MODEL_REPO,
17
- filename=MODEL_FILE,
18
- )
19
-
20
-
21
  N_THREADS = int(os.getenv("N_THREADS", "8"))
22
  N_CTX = int(os.getenv("N_CTX", "2048"))
23
  N_BATCH = int(os.getenv("N_BATCH", "256"))
24
 
25
  llm = None
26
 
27
- # =========================
28
- # FASTAPI LIFESPAN
29
- # =========================
30
  @asynccontextmanager
31
  async def lifespan(app: FastAPI):
32
  global llm
 
 
 
 
 
 
33
  llm = Llama(
34
- model_path=MODEL_PATH,
35
- n_ctx=N_CTX,
36
- n_threads=N_THREADS,
37
- n_batch=N_BATCH,
38
- verbose=False,
39
  )
40
 
41
  yield
42
 
43
- app = FastAPI(title="AI Humanizer", lifespan=lifespan)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
- # =========================
46
  # INPUT SCHEMA
47
- # =========================
48
  class HumanizeRequest(BaseModel):
49
  text: str
50
  section: str # abstract | introduction | methodology | results | discussion
51
  author_notes: str | None = None
52
 
53
- # =========================
54
- # SECTION-AWARE STYLE MAP
55
- # =========================
56
  SECTION_STYLE = {
57
- "abstract": "Write concisely, densely, and objectively. Avoid narrative tone.",
58
- "introduction": "Use contextual and motivational academic tone. Explain relevance.",
59
- "methodology": "Be procedural, precise, and restrained. No persuasion.",
60
- "results": "Be cautious, numerical, and observational. No strong claims.",
61
  "discussion": "Be interpretive, reflective, and analytical."
62
  }
63
 
64
- # =========================
65
  # PROMPT BUILDER
66
- # =========================
67
  def build_prompt(text: str, section: str, author_notes: str | None):
68
  style = SECTION_STYLE.get(section.lower(), "Use formal academic tone.")
69
 
70
  notes_block = (
71
- f"\nAuthor context (do not invent new reasoning):\n{author_notes}\n"
72
  if author_notes else ""
73
  )
74
 
@@ -93,7 +107,7 @@ HUMANIZATION RULES:
93
  - Reorder clauses where appropriate
94
  - Avoid overused academic fillers (e.g., "Moreover", "Furthermore", "It is important to note")
95
  - Prefer implicit transitions over explicit signposting
96
- - Preserve and highlight existing author reasoning or constraints
97
  - Use controlled lexical variation without replacing technical terms
98
 
99
  {notes_block}
@@ -108,9 +122,9 @@ OUTPUT:
108
  Return ONLY the humanized text.
109
  """.strip()
110
 
111
- # =========================
112
- # API ENDPOINT
113
- # =========================
114
  @app.post("/humanize")
115
  def humanize(req: HumanizeRequest):
116
  if not req.text.strip():
@@ -124,8 +138,8 @@ def humanize(req: HumanizeRequest):
124
  temperature=0.4,
125
  top_p=0.9,
126
  repetition_penalty=1.1,
127
- stop=["<|end|>"]
128
  )
129
 
130
- output = response["choices"][0]["text"].strip()
131
- return {"humanized_text": output}
 
 
5
  from contextlib import asynccontextmanager
6
  from huggingface_hub import hf_hub_download
7
 
8
+ # ==================================================
9
+ # MODEL CONFIGURATION
10
+ # ==================================================
 
11
  MODEL_REPO = "bartowski/Phi-3.1-mini-4k-instruct-GGUF"
12
  MODEL_FILE = "Phi-3.1-mini-4k-instruct-IQ2_M.gguf"
13
 
 
 
 
 
 
 
14
  N_THREADS = int(os.getenv("N_THREADS", "8"))
15
  N_CTX = int(os.getenv("N_CTX", "2048"))
16
  N_BATCH = int(os.getenv("N_BATCH", "256"))
17
 
18
  llm = None
19
 
20
+ # ==================================================
21
+ # FASTAPI LIFESPAN (MODEL LOAD)
22
+ # ==================================================
23
  @asynccontextmanager
24
  async def lifespan(app: FastAPI):
25
  global llm
26
+
27
+ model_path = hf_hub_download(
28
+ repo_id=MODEL_REPO,
29
+ filename=MODEL_FILE,
30
+ )
31
+
32
  llm = Llama(
33
+ model_path=model_path,
34
+ n_ctx=N_CTX,
35
+ n_threads=N_THREADS,
36
+ n_batch=N_BATCH,
37
+ verbose=False,
38
  )
39
 
40
  yield
41
 
42
+ # ==================================================
43
+ # APP INIT
44
+ # ==================================================
45
+ app = FastAPI(
46
+ title="AI Humanizer",
47
+ description="Academic-safe AI Humanizer",
48
+ version="1.0",
49
+ lifespan=lifespan
50
+ )
51
+
52
+ # ==================================================
53
+ # DUMMY AUTH VERIFY (Frontend Fix)
54
+ # ==================================================
55
+ @app.get("/api/auth/verify")
56
+ def verify_auth():
57
+ return {"authenticated": True}
58
 
59
+ # ==================================================
60
  # INPUT SCHEMA
61
+ # ==================================================
62
  class HumanizeRequest(BaseModel):
63
  text: str
64
  section: str # abstract | introduction | methodology | results | discussion
65
  author_notes: str | None = None
66
 
67
+ # ==================================================
68
+ # SECTION STYLE MAP
69
+ # ==================================================
70
  SECTION_STYLE = {
71
+ "abstract": "Write concisely and densely with objective academic tone.",
72
+ "introduction": "Use contextual, motivational academic tone.",
73
+ "methodology": "Be procedural, precise, and restrained.",
74
+ "results": "Be cautious, numerical, and observational.",
75
  "discussion": "Be interpretive, reflective, and analytical."
76
  }
77
 
78
+ # ==================================================
79
  # PROMPT BUILDER
80
+ # ==================================================
81
  def build_prompt(text: str, section: str, author_notes: str | None):
82
  style = SECTION_STYLE.get(section.lower(), "Use formal academic tone.")
83
 
84
  notes_block = (
85
+ f"\nAuthor context (do not invent reasoning):\n{author_notes}\n"
86
  if author_notes else ""
87
  )
88
 
 
107
  - Reorder clauses where appropriate
108
  - Avoid overused academic fillers (e.g., "Moreover", "Furthermore", "It is important to note")
109
  - Prefer implicit transitions over explicit signposting
110
+ - Preserve existing author reasoning and constraints
111
  - Use controlled lexical variation without replacing technical terms
112
 
113
  {notes_block}
 
122
  Return ONLY the humanized text.
123
  """.strip()
124
 
125
+ # ==================================================
126
+ # HUMANIZE ENDPOINT
127
+ # ==================================================
128
  @app.post("/humanize")
129
  def humanize(req: HumanizeRequest):
130
  if not req.text.strip():
 
138
  temperature=0.4,
139
  top_p=0.9,
140
  repetition_penalty=1.1,
 
141
  )
142
 
143
+ return {
144
+ "humanized_text": response["choices"][0]["text"].strip()
145
+ }