Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,39 +2,43 @@ import torch
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import gradio as gr
|
4 |
import json
|
|
|
5 |
|
|
|
6 |
model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
8 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
9 |
|
10 |
-
#
|
11 |
with open("memory_questions.json", "r") as f:
|
12 |
memory_data = json.load(f)
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
18 |
|
|
|
19 |
def generate_question(memory):
|
20 |
-
prompt =
|
21 |
-
Memory: {memory}
|
22 |
-
Question:"""
|
23 |
-
|
24 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
25 |
output = model.generate(input_ids, max_new_tokens=50, do_sample=False)
|
26 |
result = tokenizer.decode(output[0], skip_special_tokens=True)
|
27 |
|
28 |
-
#
|
29 |
lines = result.strip().split("Question:")
|
30 |
return lines[-1].strip() if len(lines) > 1 else result.strip()
|
31 |
|
32 |
-
# Gradio
|
33 |
iface = gr.Interface(
|
34 |
fn=generate_question,
|
35 |
inputs=gr.Textbox(label="Your Memory"),
|
36 |
outputs=gr.Textbox(label="Generated Question"),
|
37 |
-
title="
|
|
|
38 |
)
|
39 |
|
40 |
iface.launch()
|
|
|
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()
|