viskav commited on
Commit
b2de65f
·
verified ·
1 Parent(s): 20397e4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -19,38 +19,32 @@ app.add_middleware(
19
 
20
  @app.post("/api/humanize")
21
  async def generate_humanized_text(request: Request):
22
- # 1. Read raw JSON body
23
  try:
24
  body = await request.json()
25
- print("Incoming JSON:", body) # shows in Space logs
26
- except Exception:
27
  raise HTTPException(status_code=400, detail="Invalid JSON")
28
 
29
- # 2. Extract "text"
30
  text = body.get("text")
31
- if not isinstance(text, str) or not text.strip():
32
- raise HTTPException(status_code=400, detail="Field 'text' is required")
33
 
34
- # 3. Build prompt and call model
35
- prompt = (
36
- "Humanize the following text, making it sound natural and engaging:\n\n"
37
- f"{text}"
38
- )
39
 
 
40
  try:
41
  response = client.text_generation(
42
  prompt,
43
  max_new_tokens=512,
44
  temperature=0.7,
45
- top_p=0.9,
46
- repetition_penalty=1.1,
47
  )
48
  except Exception as e:
49
- print("HF error:", e)
50
- raise HTTPException(status_code=500, detail=f"HF error: {e}")
51
 
52
  return {"result": response}
53
 
54
  @app.get("/")
55
- def health_check():
56
- return {"status": "ok", "message": f"Humanizer API using {MODEL_ID}"}
 
19
 
20
  @app.post("/api/humanize")
21
  async def generate_humanized_text(request: Request):
22
+ # 1. Read JSON body
23
  try:
24
  body = await request.json()
25
+ except:
 
26
  raise HTTPException(status_code=400, detail="Invalid JSON")
27
 
28
+ # 2. Extract field
29
  text = body.get("text")
30
+ if not text:
31
+ raise HTTPException(status_code=400, detail="Missing 'text' field")
32
 
33
+ # 3. Build prompt
34
+ prompt = f"Humanize the following text:\n\n{text}"
 
 
 
35
 
36
+ # 4. Generate output
37
  try:
38
  response = client.text_generation(
39
  prompt,
40
  max_new_tokens=512,
41
  temperature=0.7,
 
 
42
  )
43
  except Exception as e:
44
+ raise HTTPException(status_code=500, detail=str(e))
 
45
 
46
  return {"result": response}
47
 
48
  @app.get("/")
49
+ def health():
50
+ return {"status": "ok"}