memorease commited on
Commit
863d80b
·
verified ·
1 Parent(s): 944fec5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -9
app.py CHANGED
@@ -1,30 +1,40 @@
1
  import torch
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
 
4
 
5
- # Model yükleme (hafif!)
6
  model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
7
-
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
  model = AutoModelForCausalLM.from_pretrained(model_id)
10
 
 
 
 
 
 
 
 
 
 
11
  def generate_question(memory):
12
- prompt = f"<|system|>You are a helpful assistant that generates meaningful questions from memories.<|user|>Memory: {memory}\nGenerate a related question.<|assistant|>"
 
 
13
 
14
  input_ids = tokenizer(prompt, return_tensors="pt").input_ids
15
- output_ids = model.generate(input_ids, max_new_tokens=50, do_sample=True)
16
- result = tokenizer.decode(output_ids[0], skip_special_tokens=True)
17
 
18
- # Cevabın son kısmını al
19
- question = result.split("<|assistant|>")[-1].strip()
20
- return question
21
 
22
  # Gradio UI
23
  iface = gr.Interface(
24
  fn=generate_question,
25
  inputs=gr.Textbox(label="Your Memory"),
26
  outputs=gr.Textbox(label="Generated Question"),
27
- title="TinyLLaMA Memory Question Generator"
28
  )
29
 
30
  iface.launch()
 
1
  import torch
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
4
+ import json
5
 
 
6
  model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
 
7
  tokenizer = AutoTokenizer.from_pretrained(model_id)
8
  model = AutoModelForCausalLM.from_pretrained(model_id)
9
 
10
+ # Örnek veri setini yükle
11
+ with open("memory_questions.json", "r") as f:
12
+ memory_data = json.load(f)
13
+
14
+ # İlk 3-4 örnekten prompt hazırla
15
+ few_shot_examples = "\n".join(
16
+ [f"Memory: {item['description']}\nQuestion: {item['question']}" for item in memory_data[:5]]
17
+ )
18
+
19
  def generate_question(memory):
20
+ prompt = f"""{few_shot_examples}
21
+ Memory: {memory}
22
+ Question:"""
23
 
24
  input_ids = tokenizer(prompt, return_tensors="pt").input_ids
25
+ output = model.generate(input_ids, max_new_tokens=50, do_sample=False)
26
+ result = tokenizer.decode(output[0], skip_special_tokens=True)
27
 
28
+ # Çıktıdan yalnızca son soruyu ayrıştır
29
+ lines = result.strip().split("Question:")
30
+ return lines[-1].strip() if len(lines) > 1 else result.strip()
31
 
32
  # Gradio UI
33
  iface = gr.Interface(
34
  fn=generate_question,
35
  inputs=gr.Textbox(label="Your Memory"),
36
  outputs=gr.Textbox(label="Generated Question"),
37
+ title="Memory-Aware Question Generator (TinyLLaMA)"
38
  )
39
 
40
  iface.launch()