| import gradio as gr |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
| import torch |
|
|
| |
| model_name = "google/gemma-3-4b-it" |
| tokenizer = AutoTokenizer.from_pretrained(model_name) |
| model = AutoModelForCausalLM.from_pretrained( |
| model_name, |
| torch_dtype=torch.float16, |
| device_map="auto" |
| ) |
|
|
| |
| system_prompt = """Sen bir sağlık danışmanısın. Kullanıcılara sağlık, beslenme, egzersiz ve genel sağlık bilgileri konusunda yardımcı olursun. Tıbbi teşhis koymaz, reçete önermez veya acil tıbbi durumlar için tavsiye vermezsin. Ciddi sağlık sorunları için her zaman bir doktora başvurmalarını önerirsin. Bilimsel ve güncel bilgilere dayanarak, anlaşılır ve yardımcı yanıtlar verirsin.""" |
|
|
| def predict(message, history): |
| |
| messages = [{"role": "system", "content": system_prompt}] |
| |
| |
| for user_msg, bot_msg in history: |
| messages.append({"role": "user", "content": user_msg}) |
| messages.append({"role": "assistant", "content": bot_msg}) |
| |
| |
| messages.append({"role": "user", "content": message}) |
| |
| |
| inputs = tokenizer.apply_chat_template( |
| messages, |
| return_tensors="pt" |
| ).to(model.device) |
| |
| |
| outputs = model.generate( |
| inputs, |
| max_new_tokens=512, |
| temperature=0.7, |
| top_p=0.95, |
| do_sample=True |
| ) |
| |
| |
| response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True) |
| |
| return response |
|
|
| |
| demo = gr.ChatInterface( |
| fn=predict, |
| title="Sağlık Danışmanı Chatbot (Gemma 3)", |
| description="Bu chatbot, sağlık, beslenme ve egzersiz konularında bilgi vermek için tasarlanmıştır. Tıbbi teşhis koymaz veya reçete önermez.", |
| theme="soft" |
| ) |
|
|
| demo.launch() |
|
|