Spaces:
Sleeping
Sleeping
import gradio as gr | |
import json | |
from sentence_transformers import SentenceTransformer | |
from sklearn.metrics.pairwise import cosine_similarity | |
import numpy as np | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
import torch | |
# 1. Embed model for semantic similarity | |
embedder = SentenceTransformer("paraphrase-MiniLM-L3-v2") # Küçük ve hızlı | |
# 2. LLM model for question generation (TinyLLaMA) | |
llm_model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" | |
tokenizer = AutoTokenizer.from_pretrained(llm_model_id) | |
model = AutoModelForCausalLM.from_pretrained(llm_model_id) | |
# 3. Load memory-question data | |
with open("memory_questions.json", "r") as f: | |
memory_data = json.load(f) | |
memory_texts = [item['description'] for item in memory_data] | |
memory_embeddings = embedder.encode(memory_texts) | |
# 4. En alakalı memory'yi bul + LLM ile soru üret | |
def generate_question(user_memory): | |
# (a) En benzer memory'yi bul | |
user_embedding = embedder.encode([user_memory]) | |
similarities = cosine_similarity(user_embedding, memory_embeddings)[0] | |
best_match_index = np.argmax(similarities) | |
matched_memory = memory_data[best_match_index]['description'] | |
# (b) Prompt hazırlığı | |
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|>" | |
# (c) LLM ile generate et | |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids | |
output = model.generate(input_ids, max_new_tokens=50, do_sample=False) | |
result = tokenizer.decode(output[0], skip_special_tokens=True) | |
# (d) Sadece son üretilen kısmı al | |
if "<|assistant|>" in result: | |
result = result.split("<|assistant|>")[-1].strip() | |
return result | |
# 5. Gradio UI | |
iface = gr.Interface( | |
fn=generate_question, | |
inputs=gr.Textbox(label="Your Memory"), | |
outputs=gr.Textbox(label="Generated Question"), | |
title="MemoRease – LLM-Enhanced Question Generator", | |
description="Enter a memory. We'll find a similar one and generate a clear, meaningful question using TinyLLaMA." | |
) | |
iface.launch() | |