ahmadbeilouni commited on
Commit
0b5c836
·
verified ·
1 Parent(s): 6dc4749

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -5,11 +5,12 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
5
  # ------------------------------
6
  # Configuration
7
  # ------------------------------
8
- MODEL_NAME = "tiiuae/falcon-7b-instruct"
9
- MAX_LENGTH = 100
10
- TEMPERATURE = 0.2
 
11
 
12
- print("🚀 Loading Falcon 7B for Damascus Real Estate...")
13
 
14
  # ------------------------------
15
  # Load model and tokenizer
@@ -19,7 +20,8 @@ try:
19
  model = AutoModelForCausalLM.from_pretrained(
20
  MODEL_NAME,
21
  torch_dtype=torch.float16,
22
- device_map="auto"
 
23
  )
24
 
25
  generator = pipeline(
@@ -30,7 +32,7 @@ try:
30
  device=0 if torch.cuda.is_available() else -1
31
  )
32
 
33
- print("✅ Falcon 7B loaded successfully")
34
  model_loaded = True
35
 
36
  except Exception as e:
@@ -57,20 +59,28 @@ test_questions = [
57
  def chat_falcon(user_input):
58
  if not model_loaded:
59
  return "❌ النموذج غير محمل. تحقق من الإعدادات."
60
- prompt = f"السؤال: {user_input}\nالجواب:"
 
 
 
61
  output = generator(
62
  prompt,
63
  max_new_tokens=MAX_LENGTH,
64
  do_sample=True,
65
- temperature=TEMPERATURE
 
 
66
  )[0]["generated_text"]
67
- return output
 
 
 
68
 
69
  # ------------------------------
70
  # Build Gradio Interface
71
  # ------------------------------
72
  with gr.Blocks() as demo:
73
- gr.Markdown("## 🏠 Falcon 7B - Damascus Real Estate Test")
74
  gr.Markdown("اختبر قدرة النموذج على فهم الأسئلة بالعربية (لهجة سورية أو فصحى)")
75
 
76
  with gr.Row():
 
5
  # ------------------------------
6
  # Configuration
7
  # ------------------------------
8
+ MODEL_NAME = "Malmarz/falcon-7b-arabic-instruct"
9
+ MAX_LENGTH = 120 # safer than 50
10
+ TEMPERATURE = 0.3
11
+ REPETITION_PENALTY = 1.8
12
 
13
+ print("🚀 Loading Falcon 7B Arabic Instruct...")
14
 
15
  # ------------------------------
16
  # Load model and tokenizer
 
20
  model = AutoModelForCausalLM.from_pretrained(
21
  MODEL_NAME,
22
  torch_dtype=torch.float16,
23
+ device_map="auto",
24
+ trust_remote_code=True
25
  )
26
 
27
  generator = pipeline(
 
32
  device=0 if torch.cuda.is_available() else -1
33
  )
34
 
35
+ print("✅ Falcon 7B Arabic model loaded successfully")
36
  model_loaded = True
37
 
38
  except Exception as e:
 
59
  def chat_falcon(user_input):
60
  if not model_loaded:
61
  return "❌ النموذج غير محمل. تحقق من الإعدادات."
62
+
63
+ # Structured prompt for clarity
64
+ prompt = f"أنت مساعد عقارات ذكي. أجب بجملة أو جملتين واضحتين.\nالسؤال: {user_input}\nالجواب:"
65
+
66
  output = generator(
67
  prompt,
68
  max_new_tokens=MAX_LENGTH,
69
  do_sample=True,
70
+ temperature=TEMPERATURE,
71
+ repetition_penalty=REPETITION_PENALTY,
72
+ top_p=0.9
73
  )[0]["generated_text"]
74
+
75
+ # Strip the prompt part from the output
76
+ cleaned_output = output.replace(prompt, "").strip()
77
+ return cleaned_output
78
 
79
  # ------------------------------
80
  # Build Gradio Interface
81
  # ------------------------------
82
  with gr.Blocks() as demo:
83
+ gr.Markdown("## 🏠 Falcon 7B Arabic Instruct - Damascus Real Estate Test")
84
  gr.Markdown("اختبر قدرة النموذج على فهم الأسئلة بالعربية (لهجة سورية أو فصحى)")
85
 
86
  with gr.Row():