Tomoniai commited on
Commit
d631036
·
verified ·
1 Parent(s): 12fa2a9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ from transformers import pipeline
3
+ import torch
4
+ import gradio as gr
5
+
6
+ base_model_name = "microsoft/Phi-3-mini-4k-instruct"
7
+ model = AutoModelForCausalLM.from_pretrained(base_model_name, torch_dtype=torch.float32, device_map="cpu", low_cpu_mem_usage=True, trust_remote_code=True)
8
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name , trust_remote_code=True)
9
+
10
+ def format_prompt(message, history):
11
+ system_prompt = "You are Phi3, a highly knowledgeable and friendly super intelligent AI assistant equipped with extensive information across various domains."
12
+ prompt = ""
13
+ prompt += f"<|system|>\n{system_prompt}<|end|>\n"
14
+ for user_prompt, bot_response in history:
15
+ prompt += f"<|user|>{user_prompt}<|end|>\n"
16
+ prompt += f"<|assistant|>{bot_response}<|end|>\n"
17
+ prompt += f"<|user|>{message}<|end|>\n<|assistant|>"
18
+ return prompt
19
+
20
+ def generate(prompt, history, max_new_tokens = 128, temperature = 0.6):
21
+
22
+ temperature = float(temperature)
23
+ if temperature < 1e-2:
24
+ temperature = 1e-2
25
+
26
+ formatted_prompt = format_prompt(prompt, history)
27
+ response = ""
28
+
29
+ # Count the number of tokens in the prompt
30
+ num_prompt_tokens = len(tokenizer(formatted_prompt)['input_ids'])
31
+ # Calculate the maximum length for the generation
32
+ max_length = num_prompt_tokens + max_new_tokens
33
+
34
+ textgen = pipeline('text-generation', model=model, tokenizer=tokenizer, max_length=max_length, temperature=temperature)
35
+ output = textgen(formatted_prompt)
36
+ response = output[0]['generated_text'].replace(formatted_prompt, '')
37
+ return response
38
+
39
+
40
+ mychatbot = gr.Chatbot(
41
+ avatar_images=["user.png", "botp.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
42
+
43
+
44
+ demo = gr.ChatInterface(fn=generate,
45
+ chatbot=mychatbot,
46
+ title="Phi-3 Mini Chat Demo",
47
+ retry_btn=None,
48
+ undo_btn=None
49
+ )
50
+
51
+ demo.queue().launch(show_api=False)