memorease commited on
Commit
830876b
·
verified ·
1 Parent(s): 5db876d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -28
app.py CHANGED
@@ -1,44 +1,34 @@
1
- import torch
2
- from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
4
  import json
5
- import random
 
 
6
 
7
- # Model yükle
8
- model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
9
- tokenizer = AutoTokenizer.from_pretrained(model_id)
10
- model = AutoModelForCausalLM.from_pretrained(model_id)
11
 
12
- # Hafızadan örnekleri yükle
13
  with open("memory_questions.json", "r") as f:
14
  memory_data = json.load(f)
15
 
16
- # Few-shot prompt oluşturan fonksiyon
17
- def get_few_shot_prompt(memory, k=5):
18
- examples = random.sample(memory_data, k)
19
- few_shot = "\n".join(
20
- [f"Memory: {ex['description']}\nQuestion: {ex['question']}" for ex in examples]
21
- )
22
- return f"{few_shot}\nMemory: {memory}\nQuestion:"
23
 
24
- # Ana fonksiyon
25
- def generate_question(memory):
26
- prompt = get_few_shot_prompt(memory)
27
- input_ids = tokenizer(prompt, return_tensors="pt").input_ids
28
- output = model.generate(input_ids, max_new_tokens=50, do_sample=False)
29
- result = tokenizer.decode(output[0], skip_special_tokens=True)
30
 
31
- # Sadece cevabı ayıkla
32
- lines = result.strip().split("Question:")
33
- return lines[-1].strip() if len(lines) > 1 else result.strip()
34
-
35
- # Gradio arayüzü
36
  iface = gr.Interface(
37
  fn=generate_question,
38
  inputs=gr.Textbox(label="Your Memory"),
39
  outputs=gr.Textbox(label="Generated Question"),
40
- title="MemoRease - TinyLLaMA Chat Generator",
41
- description="Write a memory, get a question to help recall more details!"
42
  )
43
 
44
  iface.launch()
 
 
 
1
  import gradio as gr
2
  import json
3
+ from sentence_transformers import SentenceTransformer
4
+ from sklearn.metrics.pairwise import cosine_similarity
5
+ import numpy as np
6
 
7
+ # 1. Küçük bir gömme (embedding) modeli
8
+ embedder = SentenceTransformer("all-MiniLM-L6-v2")
 
 
9
 
10
+ # 2. Verileri yükle
11
  with open("memory_questions.json", "r") as f:
12
  memory_data = json.load(f)
13
 
14
+ # 3. Tüm memory'leri embed et (başta bir defa)
15
+ memory_texts = [item['description'] for item in memory_data]
16
+ memory_embeddings = embedder.encode(memory_texts)
 
 
 
 
17
 
18
+ # 4. Soru üretme fonksiyonu
19
+ def generate_question(user_memory):
20
+ user_embedding = embedder.encode([user_memory])
21
+ similarities = cosine_similarity(user_embedding, memory_embeddings)[0]
22
+ best_match_index = np.argmax(similarities)
23
+ return memory_data[best_match_index]['question']
24
 
25
+ # 5. Gradio Arayüzü
 
 
 
 
26
  iface = gr.Interface(
27
  fn=generate_question,
28
  inputs=gr.Textbox(label="Your Memory"),
29
  outputs=gr.Textbox(label="Generated Question"),
30
+ title="MemoRease - Semantic Memory Question Generator",
31
+ description="Write a memory, get the most semantically related question from your dataset!"
32
  )
33
 
34
  iface.launch()