Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import torch
|
4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer, pipeline
|
5 |
+
from threading import Thread
|
6 |
+
|
7 |
+
# The huggingface model id for phi-1_5 instruct model
|
8 |
+
checkpoint = "rasyosef/Phi-1_5-Instruct-v0.1"
|
9 |
+
|
10 |
+
# Download and load model and tokenizer
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(checkpoint)
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(checkpoint, torch_dtype=torch.float32, device_map="auto")
|
13 |
+
|
14 |
+
# Text generation pipeline
|
15 |
+
phi1_5 = pipeline(
|
16 |
+
"text-generation",
|
17 |
+
tokenizer=tokenizer,
|
18 |
+
model=model,
|
19 |
+
pad_token_id=tokenizer.eos_token_id,
|
20 |
+
eos_token_id=[tokenizer.eos_token_id],
|
21 |
+
device_map="cpu"
|
22 |
+
)
|
23 |
+
|
24 |
+
# Function that accepts a prompt and generates text using the phi2 pipeline
|
25 |
+
def generate(message, chat_history, max_new_tokens):
|
26 |
+
|
27 |
+
history = [
|
28 |
+
{"role": "system", "content": "You are Phi, a helpful AI assistant made by Microsoft and RasYosef. User will you give you a task. Your goal is to complete the task as faithfully as you can."}
|
29 |
+
]
|
30 |
+
|
31 |
+
for sent, received in chat_history:
|
32 |
+
history.append({"role": "user", "content": sent})
|
33 |
+
history.append({"role": "assistant", "content": received})
|
34 |
+
|
35 |
+
history.append({"role": "user", "content": message})
|
36 |
+
#print(history)
|
37 |
+
|
38 |
+
if len(tokenizer.apply_chat_template(history)) > 512:
|
39 |
+
yield "chat history is too long"
|
40 |
+
else:
|
41 |
+
# Streamer
|
42 |
+
streamer = TextIteratorStreamer(tokenizer=tokenizer, skip_prompt=True, skip_special_tokens=True, timeout=300.0)
|
43 |
+
thread = Thread(target=phi1_5, kwargs={"text_inputs":history, "max_new_tokens":max_new_tokens, "streamer":streamer})
|
44 |
+
thread.start()
|
45 |
+
|
46 |
+
generated_text = ""
|
47 |
+
for word in streamer:
|
48 |
+
generated_text += word
|
49 |
+
response = generated_text.strip()
|
50 |
+
|
51 |
+
yield response
|
52 |
+
|
53 |
+
# Chat interface with gradio
|
54 |
+
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("""
|
56 |
+
# Phi-1_5 Chatbot Demo
|
57 |
+
This chatbot was created using a finetuned version of Microsoft's 1.4 billion parameter Phi 1.5 transformer model, [Phi-1_5-Instruct-v0.1](https://huggingface.co/rasyosef/Phi-1_5-Instruct-v0.1).
|
58 |
+
""")
|
59 |
+
|
60 |
+
tokens_slider = gr.Slider(8, 256, value=64, label="Maximum new tokens", info="A larger `max_new_tokens` parameter value gives you longer text responses but at the cost of a slower response time.")
|
61 |
+
|
62 |
+
chatbot = gr.ChatInterface(
|
63 |
+
chatbot=gr.Chatbot(height=400),
|
64 |
+
fn=generate,
|
65 |
+
additional_inputs=[tokens_slider],
|
66 |
+
stop_btn=None,
|
67 |
+
examples=[
|
68 |
+
["Translate the word 'cat' to German."],
|
69 |
+
["Recommend me three animated movies."],
|
70 |
+
["Solve this quadratic equation.\n\nx^2 + 3x + 2 = 0\nwhat is the value of x?"],
|
71 |
+
["Implement Euclid's GCD Algorithm in python"],
|
72 |
+
["Molly and Abigail want to attend a beauty and modeling contest. They both want to buy new pairs of shoes and dresses. Molly buys a pair of shoes which costs $40 and a dress which costs $160. How much should Abigail budget if she wants to spend half of what Molly spent on the pair of shoes and dress?"],
|
73 |
+
]
|
74 |
+
)
|
75 |
+
|
76 |
+
demo.queue().launch(debug=True)
|