diginoron commited on
Commit
1c35cc8
·
verified ·
1 Parent(s): 75d2d9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -6,7 +6,7 @@ import gradio as gr
6
  import datetime
7
  import os
8
 
9
- # 🟡 ورود به حساب Hugging Face با استفاده از سکرت
10
  hf_token = os.environ.get("new_hf")
11
  login(token=hf_token)
12
 
@@ -21,7 +21,7 @@ label_map = {
21
  6: "میگرن"
22
  }
23
 
24
- # مدل تشخیص بیماری (BERT)
25
  bert_model_id = "diginoron/bert-medical-fa"
26
  bert_tokenizer = AutoTokenizer.from_pretrained(bert_model_id)
27
  bert_model = AutoModelForSequenceClassification.from_pretrained(bert_model_id)
@@ -37,7 +37,7 @@ gemma_model = AutoModelForCausalLM.from_pretrained(
37
  )
38
  gemma_model.eval()
39
 
40
- # تابع تحلیل علائم و تولید توضیح
41
  def predict_and_explain(symptoms):
42
  inputs = bert_tokenizer(symptoms, return_tensors="pt")
43
  with torch.no_grad():
@@ -48,7 +48,7 @@ def predict_and_explain(symptoms):
48
  prediction = prediction.item()
49
 
50
  if confidence < 0.5:
51
- return "❌ تشخیص قطعی داده نشد. لطفاً با پزشک مشورت کنید.", ""
52
 
53
  diagnosis = label_map[prediction]
54
  diagnosis_text = f"✅ احتمالاً شما دچار {diagnosis} هستید. (اطمینان: {round(confidence * 100, 1)}٪)"
@@ -62,16 +62,17 @@ def predict_and_explain(symptoms):
62
  with torch.no_grad():
63
  output = gemma_model.generate(
64
  **inputs,
65
- max_new_tokens=300,
66
  do_sample=True,
67
  top_p=0.9,
68
- temperature=0.7
 
69
  )
70
 
71
  explanation = gemma_tokenizer.decode(output[0], skip_special_tokens=True)
72
  explanation = explanation.replace(prompt, "").strip()
73
 
74
- return diagnosis_text, explanation
75
 
76
  # رابط Gradio
77
  with gr.Blocks(css="body { font-family: Vazirmatn, sans-serif; background-color: #111827; color: #f3f4f6; } .gr-button { font-weight: bold; }") as demo:
@@ -79,16 +80,25 @@ with gr.Blocks(css="body { font-family: Vazirmatn, sans-serif; background-color:
79
 
80
  with gr.Row():
81
  inp = gr.Textbox(placeholder="مثلاً: سرفه خشک، تب، گلودرد", label="علائم شما")
 
82
  with gr.Row():
83
  out1 = gr.Textbox(label="نتیجه تشخیص اولیه")
84
  with gr.Row():
85
  out2 = gr.Textbox(label="توضیح بیماری و پیشنهادات اولیه")
86
 
 
 
 
87
  btn = gr.Button("🔍 بررسی و تحلیل علائم")
88
  clr = gr.Button("🧹 پاک کردن همه موارد")
89
 
90
- btn.click(fn=predict_and_explain, inputs=inp, outputs=[out1, out2])
91
- clr.click(fn=lambda: ("", "", ""), inputs=[], outputs=[inp, out1, out2])
 
 
 
 
 
92
 
93
  gr.Markdown(f"""
94
  ---
 
6
  import datetime
7
  import os
8
 
9
+ # ورود با سکرت
10
  hf_token = os.environ.get("new_hf")
11
  login(token=hf_token)
12
 
 
21
  6: "میگرن"
22
  }
23
 
24
+ # مدل BERT تشخیص بیماری
25
  bert_model_id = "diginoron/bert-medical-fa"
26
  bert_tokenizer = AutoTokenizer.from_pretrained(bert_model_id)
27
  bert_model = AutoModelForSequenceClassification.from_pretrained(bert_model_id)
 
37
  )
38
  gemma_model.eval()
39
 
40
+ # تابع اصلی با Indicator
41
  def predict_and_explain(symptoms):
42
  inputs = bert_tokenizer(symptoms, return_tensors="pt")
43
  with torch.no_grad():
 
48
  prediction = prediction.item()
49
 
50
  if confidence < 0.5:
51
+ return "❌ تشخیص قطعی داده نشد. لطفاً با پزشک مشورت کنید.", "", gr.update(visible=False)
52
 
53
  diagnosis = label_map[prediction]
54
  diagnosis_text = f"✅ احتمالاً شما دچار {diagnosis} هستید. (اطمینان: {round(confidence * 100, 1)}٪)"
 
62
  with torch.no_grad():
63
  output = gemma_model.generate(
64
  **inputs,
65
+ max_new_tokens=150,
66
  do_sample=True,
67
  top_p=0.9,
68
+ temperature=0.7,
69
+ use_cache=True
70
  )
71
 
72
  explanation = gemma_tokenizer.decode(output[0], skip_special_tokens=True)
73
  explanation = explanation.replace(prompt, "").strip()
74
 
75
+ return diagnosis_text, explanation, gr.update(visible=False)
76
 
77
  # رابط Gradio
78
  with gr.Blocks(css="body { font-family: Vazirmatn, sans-serif; background-color: #111827; color: #f3f4f6; } .gr-button { font-weight: bold; }") as demo:
 
80
 
81
  with gr.Row():
82
  inp = gr.Textbox(placeholder="مثلاً: سرفه خشک، تب، گلودرد", label="علائم شما")
83
+
84
  with gr.Row():
85
  out1 = gr.Textbox(label="نتیجه تشخیص اولیه")
86
  with gr.Row():
87
  out2 = gr.Textbox(label="توضیح بیماری و پیشنهادات اولیه")
88
 
89
+ with gr.Row():
90
+ status = gr.Markdown("⏳ لطفاً منتظر بمانید...", visible=False)
91
+
92
  btn = gr.Button("🔍 بررسی و تحلیل علائم")
93
  clr = gr.Button("🧹 پاک کردن همه موارد")
94
 
95
+ # هنگام کلیک، وضعیت به visible و پس از اجرا به invisible
96
+ def wrapper(symptoms):
97
+ status.update(visible=True)
98
+ return predict_and_explain(symptoms)
99
+
100
+ btn.click(fn=predict_and_explain, inputs=inp, outputs=[out1, out2, status])
101
+ clr.click(fn=lambda: ("", "", gr.update(visible=False)), inputs=[], outputs=[inp, out1, out2, status])
102
 
103
  gr.Markdown(f"""
104
  ---