Mr-Help commited on
Commit
6cef2a4
Β·
verified Β·
1 Parent(s): a42f9f6

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +51 -11
main.py CHANGED
@@ -1,15 +1,55 @@
1
- from fastapi import FastAPI, Request
 
2
 
3
- app = FastAPI()
4
 
5
- @app.post("/receive")
6
- async def receive_data(request: Request):
7
- try:
8
- data = await request.json()
9
- except:
10
- data = await request.body()
11
 
12
- print("πŸ“₯ RECEIVED ON HF BACKEND:")
13
- print(data)
 
14
 
15
- return {"status": "ok", "received": data}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
 
4
+ MODEL_NAME = "Qwen/Qwen2.5-7B-Instruct"
5
 
6
+ def main():
7
+ # Load tokenizer
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
 
 
 
9
 
10
+ # Pick dtype Ω…Ω†Ψ§Ψ³Ψ¨: bfloat16 Ω„Ωˆ GPU Ω…Ψͺاح، غير ΩƒΨ―Ω‡ float32 ΨΉΩ„Ω‰ CPU
11
+ has_cuda = torch.cuda.is_available()
12
+ dtype = torch.bfloat16 if has_cuda else torch.float32
13
 
14
+ # Load model (device_map="auto" يوزع ΨͺΩ„Ω‚Ψ§Ψ¦ΩŠ)
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ MODEL_NAME,
17
+ torch_dtype=dtype,
18
+ device_map="auto"
19
+ )
20
+
21
+ # Prompt: explain Past Simple in simple English
22
+ messages = [
23
+ {"role": "system", "content": "You are a friendly English teacher. Explain clearly and simply."},
24
+ {"role": "user", "content": "Explain the Past Simple tense in very simple English. Give rules and 8 short examples. Keep it clear for A2 learners."}
25
+ ]
26
+
27
+ # Convert chat messages to model input
28
+ text = tokenizer.apply_chat_template(
29
+ messages,
30
+ tokenize=False,
31
+ add_generation_prompt=True
32
+ )
33
+
34
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
35
+
36
+ # Generate
37
+ with torch.no_grad():
38
+ generated_ids = model.generate(
39
+ **model_inputs,
40
+ max_new_tokens=400,
41
+ do_sample=True,
42
+ temperature=0.7,
43
+ top_p=0.9
44
+ )
45
+
46
+ # Keep only the newly generated tokens (remove the prompt tokens)
47
+ new_tokens = generated_ids[0, model_inputs["input_ids"].shape[-1]:]
48
+ response = tokenizer.decode(new_tokens, skip_special_tokens=True)
49
+
50
+ print("\n=== Model Response ===\n")
51
+ print(response.strip())
52
+ print("\n======================\n")
53
+
54
+ if __name__ == "__main__":
55
+ main()