Coots commited on
Commit
1ddc0ed
·
verified ·
1 Parent(s): 28bbc6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -7
app.py CHANGED
@@ -5,7 +5,7 @@ from fastapi.staticfiles import StaticFiles
5
  from pathlib import Path
6
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
 
8
- # Load a decoder-only model like Falcon
9
  model_id = "tiiuae/falcon-rw-1b"
10
  tokenizer = AutoTokenizer.from_pretrained(model_id)
11
  model = AutoModelForCausalLM.from_pretrained(model_id)
@@ -15,15 +15,15 @@ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
15
  # Init FastAPI app
16
  app = FastAPI()
17
 
18
- # Mount static directory (serves script.js, index.html)
19
  app.mount("/static", StaticFiles(directory="."), name="static")
20
 
21
- # Serve the frontend page
22
  @app.get("/", response_class=HTMLResponse)
23
  async def serve_page():
24
  return HTMLResponse(Path("index.html").read_text())
25
 
26
- # Chatbot API route
27
  @app.post("/api")
28
  async def ask_ai(request: Request):
29
  data = await request.json()
@@ -33,9 +33,8 @@ async def ask_ai(request: Request):
33
  return JSONResponse(content={"answer": "❗ Please enter a valid question."})
34
 
35
  try:
36
- # Decoder-only models generate continuations; use prompt format
37
- prompt = f"[INST] {question} [/INST]"
38
- output = pipe(prompt, max_new_tokens=256)[0]["generated_text"]
39
  return JSONResponse(content={"answer": output.strip()})
40
  except Exception as e:
41
  return JSONResponse(content={"answer": f"⚠️ Error: {str(e)}"})
 
5
  from pathlib import Path
6
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
7
 
8
+ # Model: Falcon-rw-1b (decoder-only, not instruction-tuned)
9
  model_id = "tiiuae/falcon-rw-1b"
10
  tokenizer = AutoTokenizer.from_pretrained(model_id)
11
  model = AutoModelForCausalLM.from_pretrained(model_id)
 
15
  # Init FastAPI app
16
  app = FastAPI()
17
 
18
+ # Mount static directory
19
  app.mount("/static", StaticFiles(directory="."), name="static")
20
 
21
+ # Serve HTML UI
22
  @app.get("/", response_class=HTMLResponse)
23
  async def serve_page():
24
  return HTMLResponse(Path("index.html").read_text())
25
 
26
+ # Chat API
27
  @app.post("/api")
28
  async def ask_ai(request: Request):
29
  data = await request.json()
 
33
  return JSONResponse(content={"answer": "❗ Please enter a valid question."})
34
 
35
  try:
36
+ # Falcon works with plain prompts
37
+ output = pipe(question, max_new_tokens=256, return_full_text=False)[0]["generated_text"]
 
38
  return JSONResponse(content={"answer": output.strip()})
39
  except Exception as e:
40
  return JSONResponse(content={"answer": f"⚠️ Error: {str(e)}"})