Fffffd commited on
Commit
dbc8671
·
verified ·
1 Parent(s): e4d6754

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -108
app.py CHANGED
@@ -1,142 +1,134 @@
1
  import json
2
  import gradio as gr
3
  import torch
4
-
5
  from transformers import AutoTokenizer, AutoModelForCausalLM
6
 
7
 
8
- MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
 
 
 
9
  DOC_FILE = "documents.jsonl"
10
 
 
11
 
12
- print("🚀 Qwen yükleniyor...")
13
 
 
 
 
 
 
14
 
15
  tokenizer = AutoTokenizer.from_pretrained(
16
  MODEL_NAME
17
  )
18
 
19
-
20
  model = AutoModelForCausalLM.from_pretrained(
21
  MODEL_NAME,
22
  torch_dtype=torch.float16,
23
  device_map="auto"
24
  )
25
 
26
-
27
- print("📚 Hafıza yükleniyor...")
28
-
29
-
30
- memory = []
31
 
32
 
33
- try:
 
 
34
 
35
- with open(
36
- DOC_FILE,
37
- "r",
38
- encoding="utf-8"
39
- ) as f:
40
 
41
- for line in f:
42
 
43
- try:
44
- data = json.loads(line)
45
 
46
- if "text" in data:
47
- memory.append(
48
- data["text"]
49
- )
50
-
51
- except:
52
- pass
53
 
 
 
 
54
 
55
- except Exception as e:
56
-
57
- print(
58
- "Hafıza yükleme hatası:",
59
- e
60
- )
61
 
62
 
63
  print(
64
- "Hafıza kayıt:",
65
- len(memory)
66
  )
67
 
68
 
69
 
70
- def search_memory(question):
71
-
72
- words = question.lower().split()
73
 
74
- results = []
75
 
 
76
 
77
- for text in memory:
78
 
79
- score = 0
80
 
81
- low = text.lower()
82
 
 
83
 
84
- for w in words:
85
 
86
- if w in low:
 
87
  score += 1
88
 
89
 
90
- if score > 0:
91
-
92
- results.append(
93
- (
94
- score,
95
- text
96
- )
97
  )
98
 
99
 
100
- results.sort(
101
  reverse=True,
102
  key=lambda x:x[0]
103
  )
104
 
105
 
106
- context = ""
107
-
108
 
109
- for item in results[:5]:
 
110
 
111
- context += "\n" + item[1]
112
 
 
113
 
114
- return context
115
 
116
 
 
 
 
117
 
118
 
119
- def chat(message):
120
 
121
 
122
- context = search_memory(
123
- message
124
- )
125
 
126
 
127
  prompt = f"""
128
- Sen Ahhaja AI'sın.
129
 
130
- Harici hafıza:
131
 
 
132
  {context}
133
 
134
 
135
- Kullanıcı:
136
  {message}
137
 
138
 
139
- Cevap:
140
  """
141
 
142
 
@@ -148,72 +140,45 @@ Cevap:
148
  ]
149
 
150
 
151
- text = tokenizer.apply_chat_template(
152
  messages,
153
- tokenize=False,
154
- add_generation_prompt=True
155
- )
156
-
157
-
158
- inputs = tokenizer(
159
- text,
160
  return_tensors="pt"
161
- ).to(
162
- model.device
163
- )
164
 
165
 
166
  output = model.generate(
167
- **inputs,
168
  max_new_tokens=512,
169
  temperature=0.7,
170
  do_sample=True
171
  )
172
 
173
 
174
- result = tokenizer.decode(
175
- output[0],
176
  skip_special_tokens=True
177
  )
178
 
179
 
180
- if "Cevap:" in result:
181
-
182
- result = result.split(
183
- "Cevap:"
184
- )[-1]
185
-
186
 
187
- return result.strip()
188
 
189
 
 
 
 
190
 
191
- with gr.Blocks() as demo:
192
 
193
-
194
- inp = gr.Textbox(
195
- label="Mesaj"
196
- )
197
-
198
-
199
- out = gr.Textbox(
200
- label="Ahhaja"
201
- )
202
-
203
-
204
- btn = gr.Button(
205
- "Gönder"
206
- )
207
-
208
-
209
- btn.click(
210
- fn=chat,
211
- inputs=inp,
212
- outputs=out,
213
- api_name="chat"
214
- )
215
 
216
 
217
  demo.launch(
218
- server_name="0.0.0.0"
 
219
  )
 
1
  import json
2
  import gradio as gr
3
  import torch
 
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
 
6
 
7
+ # =========================
8
+ # AYARLAR
9
+ # =========================
10
+
11
  DOC_FILE = "documents.jsonl"
12
 
13
+ MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
14
 
 
15
 
16
+ # =========================
17
+ # QWEN YÜKLEME
18
+ # =========================
19
+
20
+ print("🚀 Qwen yükleniyor...")
21
 
22
  tokenizer = AutoTokenizer.from_pretrained(
23
  MODEL_NAME
24
  )
25
 
 
26
  model = AutoModelForCausalLM.from_pretrained(
27
  MODEL_NAME,
28
  torch_dtype=torch.float16,
29
  device_map="auto"
30
  )
31
 
32
+ print("✅ Qwen hazır")
 
 
 
 
33
 
34
 
35
+ # =========================
36
+ # DOKÜMAN YÜKLEME
37
+ # =========================
38
 
39
+ print("📚 Bilgi kütüphanesi yükleniyor...")
 
 
 
 
40
 
41
+ documents = []
42
 
43
+ with open(DOC_FILE, "r", encoding="utf-8") as f:
 
44
 
45
+ for line in f:
46
+ try:
47
+ item = json.loads(line)
 
 
 
 
48
 
49
+ documents.append(
50
+ item["text"]
51
+ )
52
 
53
+ except:
54
+ pass
 
 
 
 
55
 
56
 
57
  print(
58
+ f"✅ {len(documents)} doküman yüklendi"
 
59
  )
60
 
61
 
62
 
63
+ # =========================
64
+ # BASİT ARAMA
65
+ # =========================
66
 
67
+ def search_docs(query, limit=5):
68
 
69
+ query_words = query.lower().split()
70
 
71
+ scores = []
72
 
 
73
 
74
+ for doc in documents:
75
 
76
+ text = doc.lower()
77
 
78
+ score = 0
79
 
80
+ for word in query_words:
81
+ if word in text:
82
  score += 1
83
 
84
 
85
+ if score:
86
+ scores.append(
87
+ (score, doc)
 
 
 
 
88
  )
89
 
90
 
91
+ scores.sort(
92
  reverse=True,
93
  key=lambda x:x[0]
94
  )
95
 
96
 
97
+ results = []
 
98
 
99
+ for score, doc in scores[:limit]:
100
+ results.append(doc)
101
 
 
102
 
103
+ return "\n\n".join(results)
104
 
 
105
 
106
 
107
+ # =========================
108
+ # CHAT
109
+ # =========================
110
 
111
 
112
+ def chat(message, history):
113
 
114
 
115
+ context = search_docs(message)
 
 
116
 
117
 
118
  prompt = f"""
119
+ Sen Ahhaja AI isimli yardımcı asistansın.
120
 
121
+ Aşağıdaki bilgi kütüphanesini kullan.
122
 
123
+ BİLGİ:
124
  {context}
125
 
126
 
127
+ KULLANICI:
128
  {message}
129
 
130
 
131
+ Cevabı sadece verilen bilgilerden yararlanarak oluştur.
132
  """
133
 
134
 
 
140
  ]
141
 
142
 
143
+ inputs = tokenizer.apply_chat_template(
144
  messages,
145
+ add_generation_prompt=True,
 
 
 
 
 
 
146
  return_tensors="pt"
147
+ ).to(model.device)
148
+
 
149
 
150
 
151
  output = model.generate(
152
+ inputs,
153
  max_new_tokens=512,
154
  temperature=0.7,
155
  do_sample=True
156
  )
157
 
158
 
159
+ answer = tokenizer.decode(
160
+ output[0][inputs.shape[-1]:],
161
  skip_special_tokens=True
162
  )
163
 
164
 
165
+ return answer
 
 
 
 
 
166
 
 
167
 
168
 
169
+ # =========================
170
+ # GRADIO
171
+ # =========================
172
 
 
173
 
174
+ demo = gr.ChatInterface(
175
+ fn=chat,
176
+ title="🚀 Ahhaja AI",
177
+ description="Qwen + documents.jsonl bilgi kütüphanesi"
178
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
 
181
  demo.launch(
182
+ server_name="0.0.0.0",
183
+ server_port=7860
184
  )