Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,53 +3,31 @@ import json
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
import numpy as np
|
| 6 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
-
import torch
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
embedder = SentenceTransformer("paraphrase-MiniLM-L3-v2")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
llm_model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 14 |
-
tokenizer = AutoTokenizer.from_pretrained(llm_model_id)
|
| 15 |
-
model = AutoModelForCausalLM.from_pretrained(llm_model_id)
|
| 16 |
-
|
| 17 |
-
# 3. Load memory-question data
|
| 18 |
with open("memory_questions.json", "r") as f:
|
| 19 |
memory_data = json.load(f)
|
| 20 |
|
| 21 |
memory_texts = [item['description'] for item in memory_data]
|
| 22 |
memory_embeddings = embedder.encode(memory_texts)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
def generate_question(user_memory):
|
| 26 |
-
# (a) En benzer memory'yi bul
|
| 27 |
user_embedding = embedder.encode([user_memory])
|
| 28 |
similarities = cosine_similarity(user_embedding, memory_embeddings)[0]
|
| 29 |
best_match_index = np.argmax(similarities)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# (b) Prompt hazırlığı
|
| 33 |
-
prompt = f"<|system|>You are a helpful assistant who asks clear, meaningful questions based on short memories.<|user|>Memory: {matched_memory}\nGenerate a question that starts with What, Why, Who, When, or How.<|assistant|>"
|
| 34 |
-
|
| 35 |
-
# (c) LLM ile generate et
|
| 36 |
-
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
| 37 |
-
output = model.generate(input_ids, max_new_tokens=50, do_sample=False)
|
| 38 |
-
result = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 39 |
-
|
| 40 |
-
# (d) Sadece son üretilen kısmı al
|
| 41 |
-
if "<|assistant|>" in result:
|
| 42 |
-
result = result.split("<|assistant|>")[-1].strip()
|
| 43 |
-
|
| 44 |
-
return result
|
| 45 |
|
| 46 |
-
#
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=generate_question,
|
| 49 |
inputs=gr.Textbox(label="Your Memory"),
|
| 50 |
-
outputs=gr.Textbox(label="
|
| 51 |
-
title="MemoRease –
|
| 52 |
-
description="Enter a memory.
|
| 53 |
)
|
| 54 |
|
| 55 |
iface.launch()
|
|
|
|
| 3 |
from sentence_transformers import SentenceTransformer
|
| 4 |
from sklearn.metrics.pairwise import cosine_similarity
|
| 5 |
import numpy as np
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Semantik model
|
| 8 |
+
embedder = SentenceTransformer("paraphrase-MiniLM-L3-v2")
|
| 9 |
|
| 10 |
+
# Veri yükle
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
with open("memory_questions.json", "r") as f:
|
| 12 |
memory_data = json.load(f)
|
| 13 |
|
| 14 |
memory_texts = [item['description'] for item in memory_data]
|
| 15 |
memory_embeddings = embedder.encode(memory_texts)
|
| 16 |
|
| 17 |
+
# Yalnızca eşleşen soruyu döndür
|
| 18 |
def generate_question(user_memory):
|
|
|
|
| 19 |
user_embedding = embedder.encode([user_memory])
|
| 20 |
similarities = cosine_similarity(user_embedding, memory_embeddings)[0]
|
| 21 |
best_match_index = np.argmax(similarities)
|
| 22 |
+
return memory_data[best_match_index]['question']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Arayüz
|
| 25 |
iface = gr.Interface(
|
| 26 |
fn=generate_question,
|
| 27 |
inputs=gr.Textbox(label="Your Memory"),
|
| 28 |
+
outputs=gr.Textbox(label="Matched Question"),
|
| 29 |
+
title="MemoRease – Smart Matched Question (No Hallucination)",
|
| 30 |
+
description="Enter a memory. You'll get the most relevant pre-written question from your dataset."
|
| 31 |
)
|
| 32 |
|
| 33 |
iface.launch()
|