memorease commited on
Commit
8c2067d
·
verified ·
1 Parent(s): 7efac98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -1,30 +1,30 @@
1
- from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
2
  import torch
 
3
  import gradio as gr
4
 
5
- # LLaMA 2 Chat modeli
6
- model_id = "meta-llama/Llama-2-7b-chat-hf"
7
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
- model = AutoModelForCausalLM.from_pretrained(
10
- model_id,
11
- torch_dtype=torch.float16, # CPU çalışıyorsan float32 olabilir
12
- device_map="auto"
13
- )
14
 
15
  def generate_question(memory):
16
- prompt = f"[INST] You are a helpful assistant. Based on this memory, generate a question that would help the user recall more details:\n\nMemory: {memory}\n\nQuestion: [/INST]"
17
- inputs = tokenizer(prompt, return_tensors="pt")
18
- outputs = model.generate(**inputs, max_new_tokens=50)
19
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
20
- return result.split("Question:")[-1].strip()
 
 
 
 
21
 
22
- # Arayüz
23
  iface = gr.Interface(
24
  fn=generate_question,
25
  inputs=gr.Textbox(label="Your Memory"),
26
  outputs=gr.Textbox(label="Generated Question"),
27
- title="LLaMA Chat Question Generator"
28
  )
29
 
30
  iface.launch()
 
 
1
  import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import gradio as gr
4
 
5
+ # Model yükleme (hafif!)
6
+ model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
7
 
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
+ model = AutoModelForCausalLM.from_pretrained(model_id)
 
 
 
 
10
 
11
  def generate_question(memory):
12
+ prompt = f"<|system|>You are a helpful assistant that generates meaningful questions from memories.<|user|>Memory: {memory}\nGenerate a related question.<|assistant|>"
13
+
14
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids
15
+ output_ids = model.generate(input_ids, max_new_tokens=50, do_sample=True)
16
+ result = tokenizer.decode(output_ids[0], skip_special_tokens=True)
17
+
18
+ # Cevabın son kısmını al
19
+ question = result.split("<|assistant|>")[-1].strip()
20
+ return question
21
 
22
+ # Gradio UI
23
  iface = gr.Interface(
24
  fn=generate_question,
25
  inputs=gr.Textbox(label="Your Memory"),
26
  outputs=gr.Textbox(label="Generated Question"),
27
+ title="TinyLLaMA Memory Question Generator"
28
  )
29
 
30
  iface.launch()