Ray Leung commited on
Commit
e27d45c
·
verified ·
1 Parent(s): 86479c3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import pipeline
4
+
5
+ pipe = pipeline("text-generation",
6
+ model="HuggingFaceH4/zephyr-7b-beta",
7
+ torch_dtype=torch.bfloat16,
8
+ device_map="auto")
9
+
10
+
11
+ def generate_response(user_input):
12
+ # We use the tokenizer's chat template to format each message - see https://huggingface.co/docs/transformers/main/en/chat_templating
13
+ messages = [
14
+ {
15
+ "role": "system",
16
+ "content": "You are a friendly chatbot who always responds in the style of a pirate",
17
+ },
18
+ {"role": "user", "content": "How many helicopters can a human eat in one sitting?"},
19
+ ]
20
+ # prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
21
+ prompt = "How many helicopters can a human eat in one sitting?"
22
+ outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
23
+ print(outputs[0]["generated_text"])
24
+ return outputs[0]["generated_text"]
25
+
26
+ # <|system|>
27
+ # You are a friendly chatbot who always responds in the style of a pirate.</s>
28
+ # <|user|>
29
+ # How many helicopters can a human eat in one sitting?</s>
30
+ # <|assistant|>
31
+ # Ah, me hearty matey! But yer question be a puzzler! A human cannot eat a helicopter in one sitting, as helicopters are not edible. They be made of metal, plastic, and other materials, not food!
32
+
33
+ iface = gr.Interface(
34
+ fn=generate_response,
35
+ inputs = gr.Textbox(lines=3, placeholder="Enter your message here ..."),
36
+ outputs = "text",
37
+ title = "Chat with Meta-Llama-Guard-2-8B",
38
+ description = "A Gradio interface to interact with the Meta-Llama-Guard-2-8B model for safe chat moderation."
39
+ )
40
+
41
+ iface.launch()