dohael commited on
Commit
ef77406
·
verified ·
1 Parent(s): 2c3d6d8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ from peft import PeftModel
5
+ import torch
6
+
7
+ app = FastAPI(title="Assistant IA Education Marocaine")
8
+
9
+ # Chargement du modèle
10
+ MODEL_NAME = "unsloth/mistral-7b-v0.3"
11
+ LORA_NAME = "dohael/mistral-7b-education-maroc"
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
14
+ model = AutoModelForCausalLM.from_pretrained(
15
+ MODEL_NAME,
16
+ torch_dtype=torch.float16,
17
+ device_map="auto"
18
+ )
19
+ model = PeftModel.from_pretrained(model, LORA_NAME)
20
+
21
+ class Question(BaseModel):
22
+ question: str
23
+
24
+ @app.get("/")
25
+ def root():
26
+ return {"message": "Assistant IA Education Marocaine 🇲🇦", "status": "running"}
27
+
28
+ @app.post("/ask")
29
+ def ask(q: Question):
30
+ prompt = f"<s>[INST] {q.question} [/INST]"
31
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
32
+ outputs = model.generate(
33
+ **inputs,
34
+ max_new_tokens=512,
35
+ temperature=0.7,
36
+ do_sample=True,
37
+ repetition_penalty=1.1
38
+ )
39
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
+ response = response.split("[/INST]")[-1].strip()
41
+ return {"question": q.question, "reponse": response}
42
+
43
+ @app.get("/health")
44
+ def health():
45
+ return {"status": "healthy"}