File size: 1,239 Bytes
7efac98
8c2067d
ac2fa94
863d80b
ac2fa94
8c2067d
7efac98
8c2067d
7efac98
863d80b
 
 
 
 
 
 
 
 
7efac98
863d80b
 
 
8c2067d
 
863d80b
 
8c2067d
863d80b
 
 
ac2fa94
8c2067d
ac2fa94
 
 
 
863d80b
ac2fa94
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr
import json

model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)

# Örnek veri setini yükle
with open("memory_questions.json", "r") as f:
    memory_data = json.load(f)

# İlk 3-4 örnekten prompt hazırla
few_shot_examples = "\n".join(
    [f"Memory: {item['description']}\nQuestion: {item['question']}" for item in memory_data[:5]]
)

def generate_question(memory):
    prompt = f"""{few_shot_examples}
Memory: {memory}
Question:"""

    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)

    # Çıktıdan yalnızca son soruyu ayrıştır
    lines = result.strip().split("Question:")
    return lines[-1].strip() if len(lines) > 1 else result.strip()

# Gradio UI
iface = gr.Interface(
    fn=generate_question,
    inputs=gr.Textbox(label="Your Memory"),
    outputs=gr.Textbox(label="Generated Question"),
    title="Memory-Aware Question Generator (TinyLLaMA)"
)

iface.launch()