Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
10 |
-
model = AutoModelForCausalLM.from_pretrained(model_id)
|
11 |
|
12 |
-
#
|
13 |
with open("memory_questions.json", "r") as f:
|
14 |
memory_data = json.load(f)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
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 |
-
#
|
25 |
-
def generate_question(
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
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 -
|
41 |
-
description="Write a memory, get
|
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()
|