viskav commited on
Commit
901db22
·
verified ·
1 Parent(s): 25b6780

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -30
app.py CHANGED
@@ -1,58 +1,58 @@
1
- import os
2
  from fastapi import FastAPI, HTTPException
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from pydantic import BaseModel
5
- from huggingface_hub import InferenceClient
6
-
7
- # --- Model & client setup ---
8
- MODEL_ID = "microsoft/Phi-3-mini-4k-instruct"
9
- HF_TOKEN = os.getenv("HF_TOKEN") # set in Space secrets
10
-
11
- client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)
 
 
 
 
 
 
 
 
12
 
13
  app = FastAPI()
14
 
15
- # --- CORS ---
16
  app.add_middleware(
17
  CORSMiddleware,
18
- allow_origins=["*"], # later restrict to your domain
19
- allow_credentials=True,
20
  allow_methods=["*"],
21
  allow_headers=["*"],
22
  )
23
 
24
- # --- Request body schema ---
25
- class HumanizeRequest(BaseModel):
26
  text: str
27
 
28
- # --- Endpoint ---
29
  @app.post("/api/humanize")
30
- async def generate_humanized_text(body: HumanizeRequest):
31
- text = body.text
32
- if not text.strip():
33
  raise HTTPException(status_code=400, detail="Text cannot be empty")
34
 
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
- # This will show up in Space logs
50
- print("HF error:", e)
51
- raise HTTPException(status_code=500, detail=f"HF error: {e}")
52
 
53
- return {"result": response}
 
 
54
 
55
- # --- Health check ---
56
  @app.get("/")
57
  def health():
58
- return {"status": "ok"}
 
 
1
  from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
+ from llama_cpp import Llama
5
+
6
+ # GGUF MODEL
7
+ MODEL_REPO = "bartowski/Phi-3.1-mini-4k-instruct-GGUF"
8
+ MODEL_FILE = "Phi-3.1-mini-4k-instruct-Q4_K_M.gguf"
9
+
10
+ print("Loading Phi-3.1 Mini GGUF model...")
11
+ llm = Llama.from_pretrained(
12
+ repo_id=MODEL_REPO,
13
+ filename=MODEL_FILE,
14
+ n_threads=4,
15
+ n_ctx=4096,
16
+ verbose=True,
17
+ )
18
+ print("Model loaded successfully.")
19
 
20
  app = FastAPI()
21
 
 
22
  app.add_middleware(
23
  CORSMiddleware,
24
+ allow_origins=["*"],
 
25
  allow_methods=["*"],
26
  allow_headers=["*"],
27
  )
28
 
29
+ class RequestBody(BaseModel):
 
30
  text: str
31
 
 
32
  @app.post("/api/humanize")
33
+ async def humanize(body: RequestBody):
34
+ text = body.text.strip()
35
+ if not text:
36
  raise HTTPException(status_code=400, detail="Text cannot be empty")
37
 
38
  prompt = (
39
+ "Humanize the following text while keeping meaning:\n\n"
40
+ f"{text}\n\nHumanized version:\n"
41
  )
42
 
43
  try:
44
+ output = llm(
45
  prompt,
46
+ max_tokens=300,
47
  temperature=0.7,
 
 
48
  )
49
+ result = output["choices"][0]["text"].strip()
50
+ return {"result": result}
 
 
51
 
52
+ except Exception as e:
53
+ print("Model error:", e)
54
+ raise HTTPException(status_code=500, detail="Model execution error")
55
 
 
56
  @app.get("/")
57
  def health():
58
+ return {"status": "ok"}