eyupipler commited on
Commit
c2f27fe
·
verified ·
1 Parent(s): b739b42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -23
app.py CHANGED
@@ -5,7 +5,6 @@ import warnings
5
 
6
  warnings.filterwarnings("ignore")
7
 
8
- # Global değişkenler
9
  model = None
10
  tokenizer = None
11
  model_loaded = False
@@ -17,16 +16,15 @@ def load_model():
17
  return True
18
 
19
  try:
20
- print("[INFO] Model yükleniyor...")
21
 
22
  model_path = "Neurazum/Lbai-1-preview"
23
 
24
  tokenizer = AutoTokenizer.from_pretrained(model_path)
25
 
26
- # CPU için optimize edilmiş yükleme
27
  model = AutoModelForCausalLM.from_pretrained(
28
  model_path,
29
- torch_dtype=torch.float32, # CPU için float32
30
  device_map="cpu",
31
  trust_remote_code=True,
32
  low_cpu_mem_usage=True
@@ -34,7 +32,7 @@ def load_model():
34
 
35
  model.eval()
36
  model_loaded = True
37
- print("[INFO] Model başarıyla yüklendi!")
38
  return True
39
 
40
  except Exception as e:
@@ -45,33 +43,44 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
45
  global model, tokenizer, model_loaded
46
 
47
  if not message or message.strip() == "":
48
- yield "Lütfen bir mesaj yazın."
49
  return
50
 
51
- # Model yüklü değilse yükle
52
  if not model_loaded:
53
- yield "⏳ Model yükleniyor, lütfen bekleyin..."
54
  if not load_model():
55
- yield "❌ Model yüklenemedi. Lütfen daha sonra tekrar deneyin."
56
  return
57
 
58
  try:
59
- # Prompt oluştur
60
  prompt = f"{system_message}\n\n"
61
 
62
  if history:
63
- for user_msg, assistant_msg in history:
64
- if user_msg:
65
- prompt += f"Patient: {user_msg}\n"
66
- if assistant_msg:
67
- prompt += f"Doctor: {assistant_msg}\n"
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  prompt += f"Patient: {message}\nDoctor:"
70
 
71
- # Tokenize
 
72
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
73
 
74
- # Generate
75
  with torch.no_grad():
76
  outputs = model.generate(
77
  **inputs,
@@ -82,25 +91,27 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
82
  pad_token_id=tokenizer.eos_token_id
83
  )
84
 
85
- # Decode
86
  full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
87
 
88
- # Sadece yeni üretilen kısmı al
89
  if "Doctor:" in full_response:
90
  response = full_response.split("Doctor:")[-1].strip()
91
  else:
92
  response = full_response[len(prompt):].strip()
93
 
94
- yield response if response else "Model yanıt üretemedi."
 
 
 
95
 
96
  except Exception as e:
 
 
97
  yield f"❌ Hata: {str(e)}"
98
 
99
- # Gradio arayüzü
100
  chatbot = gr.ChatInterface(
101
  fn=respond,
102
- title="🩺 Medical AI Assistant",
103
- description="Tıbbi sorularınızı sorun. İlk mesajda model yüklenecek, biraz bekleyin.",
104
  additional_inputs=[
105
  gr.Textbox(
106
  value="You are a helpful medical assistant.",
 
5
 
6
  warnings.filterwarnings("ignore")
7
 
 
8
  model = None
9
  tokenizer = None
10
  model_loaded = False
 
16
  return True
17
 
18
  try:
19
+ print("[INFO] Model loading...")
20
 
21
  model_path = "Neurazum/Lbai-1-preview"
22
 
23
  tokenizer = AutoTokenizer.from_pretrained(model_path)
24
 
 
25
  model = AutoModelForCausalLM.from_pretrained(
26
  model_path,
27
+ torch_dtype=torch.float32,
28
  device_map="cpu",
29
  trust_remote_code=True,
30
  low_cpu_mem_usage=True
 
32
 
33
  model.eval()
34
  model_loaded = True
35
+ print("[INFO] Model successfully loaded!")
36
  return True
37
 
38
  except Exception as e:
 
43
  global model, tokenizer, model_loaded
44
 
45
  if not message or message.strip() == "":
46
+ yield "Please write a message..."
47
  return
48
 
 
49
  if not model_loaded:
50
+ yield "⏳ Loading model, please wait..."
51
  if not load_model():
52
+ yield "❌ Model could not be loaded. Please try again later."
53
  return
54
 
55
  try:
 
56
  prompt = f"{system_message}\n\n"
57
 
58
  if history:
59
+ for item in history:
60
+ try:
61
+ if isinstance(item, (list, tuple)) and len(item) >= 2:
62
+ user_msg, assistant_msg = item[0], item[1]
63
+ if user_msg:
64
+ prompt += f"Patient: {user_msg}\n"
65
+ if assistant_msg:
66
+ prompt += f"Doctor: {assistant_msg}\n"
67
+ elif isinstance(item, dict):
68
+ role = item.get("role", "")
69
+ content = item.get("content", "")
70
+ if role == "user" and content:
71
+ prompt += f"Patient: {content}\n"
72
+ elif role == "assistant" and content:
73
+ prompt += f"Doctor: {content}\n"
74
+ except Exception as e:
75
+ print(f"[WARNING] History item skipped: {e}")
76
+ continue
77
 
78
  prompt += f"Patient: {message}\nDoctor:"
79
 
80
+ print(f"[DEBUG] Prompt: {prompt[:300]}...")
81
+
82
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
83
 
 
84
  with torch.no_grad():
85
  outputs = model.generate(
86
  **inputs,
 
91
  pad_token_id=tokenizer.eos_token_id
92
  )
93
 
 
94
  full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
95
 
 
96
  if "Doctor:" in full_response:
97
  response = full_response.split("Doctor:")[-1].strip()
98
  else:
99
  response = full_response[len(prompt):].strip()
100
 
101
+ if "\nPatient:" in response:
102
+ response = response.split("\nPatient:")[0].strip()
103
+
104
+ yield response if response else "The model could not generate a response."
105
 
106
  except Exception as e:
107
+ import traceback
108
+ print(f"[ERROR] {traceback.format_exc()}")
109
  yield f"❌ Hata: {str(e)}"
110
 
 
111
  chatbot = gr.ChatInterface(
112
  fn=respond,
113
+ title="Lbai-1-preview",
114
+ description="Ask your medical questions. The model will load in the first message, so please wait a moment. Artificial intelligence can make mistakes.",
115
  additional_inputs=[
116
  gr.Textbox(
117
  value="You are a helpful medical assistant.",