anasmkh commited on
Commit
1f2129b
·
verified ·
1 Parent(s): 71b1646

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -12
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
 
4
  model_name = "anasmkh/customized_llama3.1_8b"
 
5
  tokenizer = AutoTokenizer.from_pretrained(model_name)
6
  model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
7
 
@@ -11,20 +12,22 @@ generator = pipeline(
11
  tokenizer=tokenizer,
12
  max_new_tokens=64,
13
  temperature=1.5,
14
- min_p=0.1
15
  )
16
 
17
- def generate_response(prompt):
18
- messages = [
19
- {"role": "user", "content": prompt},
20
- ]
21
- response = generator(messages)[0]['generated_text']
22
- return response.split("<|end_header_id|>")[1].strip()
23
 
24
- demo = gr.Interface(
25
- fn=generate_response,
26
- inputs=gr.Textbox(lines=5, label="Enter your prompt"),
27
- outputs=gr.Textbox(label="Model Response")
28
- )
 
 
29
 
30
  demo.launch()
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
 
4
  model_name = "anasmkh/customized_llama3.1_8b"
5
+
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
8
 
 
12
  tokenizer=tokenizer,
13
  max_new_tokens=64,
14
  temperature=1.5,
15
+ min_p=0.1,
16
  )
17
 
18
+ def chat(message, history):
19
+ history = history or []
20
+ history.append({"role": "user", "content": message})
21
+ response = generator(history)[-1]["generated_text"]
22
+ history.append({"role": "assistant", "content": response})
23
+ return history
24
 
25
+ with gr.Blocks() as demo:
26
+ chatbot = gr.Chatbot()
27
+ message = gr.Textbox()
28
+ clear = gr.ClearButton([message, chatbot])
29
+
30
+ message.submit(chat, [message, chatbot], chatbot)
31
+ clear.click(lambda: None, None, chatbot, queue=False)
32
 
33
  demo.launch()