Views
No views yet
Experimental: This experimental model is not suitable for real medical use. It can hallucinate and generate dangerous answers. Further medical evaluation is needed.
1from peft import AutoPeftModelForCausalLM
2from transformers import GenerationConfig, AutoTokenizer
3import torch
4import time
5
6
7def generate_response(input_text: str) -> str:
8 """
9 Generate a response for the given input text using the Typhoon-7B model.
10
11 Parameters:
12 input_text (str): The input text prompt.
13
14 Returns:
15 str: The generated response.
16 """
17 # Initialize the tokenizer and model only once
18 tokenizer = AutoTokenizer.from_pretrained("Thaweewat/typhoon-7b-chat-pobpad")
19
20 model = AutoPeftModelForCausalLM.from_pretrained(
21 "Thaweewat/typhoon-7b-chat-pobpad",
22 low_cpu_mem_usage=True,
23 return_dict=True,
24 torch_dtype=torch.float16,
25 device_map="cuda")
26
27 generation_config = GenerationConfig(
28 do_sample=True,
29 top_k=1,
30 temperature=0.4, # After a few experiment I found that between 0.3-0.4 seem to generate well
31 max_new_tokens=300,
32 repetition_penalty=1.1,
33 pad_token_id=tokenizer.eos_token_id)
34
35 # Tokenize input
36 inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
37
38 # Generate outputs
39 st_time = time.time()
40 outputs = model.generate(**inputs, generation_config=generation_config)
41
42 # Decode and print response
43 response = tokenizer.decode(outputs[0], skip_special_tokens=True)
44 print(f"Response time: {time.time() - st_time} seconds")
45 return response
46
47# Sample usage:
48# Example from https://pantip.com/topic/42422619
49input_text = """###Human: สวัสดีค่า มาตามหาอาหารเสริม คุณเเม่อายุ50+ค่ะ
50ทำงานเดินทั้งวันเเถมพักผ่อนน้อยมีบางช่วงที่ดูไม่ค่อยสดใส อยากให้ทุกคนช่วยเเนะนำอาหารเสริมหน่อยค่า
51###Assistant: """
52print(generate_response(input_text))
53
54"""
55อาหารเสริมสำหรับผู้สูงอายุ ควรเลือกทานที่มีวิตามินและแร่ธาตุครบถ้วน เช่น วิตามินบีรวม วิตามินซี แคลเซียม
56 แมกนีเซียม เหล็ก โฟเลต เป็นต้น ซึ่งจะช่วยให้ร่างกายแข็งแรงขึ้น และควรหลีกเลี่ยงการรับประทานอาหารประเภทไขมันสูง
57 เพราะอาจทำให้เกิดโรคหัวใจได้หากต้องการทราบข้อมูลเพิ่มเติม สามารถสอบถามเภสัชกรหรือแพทย์ประจำตัวเพื่อขอคำแนะนำในการดูแลสุขภาพก่อนนะคะ
58 ขอเป็นกำลังใจให้นะคะ หากมีข้อสงสัยสามารถปรึกษาพยาบาลสายด่วน โทร.1669 ได้ตลอดเวลาค่ะ
59*หมายเหตุ : การใช้ยาและการปรับเปลี่ยนพฤติกรรมต่างๆ ควรอยู่ภายใต้การดูแลของบุคลากรทางการแพทย์*
60"""