Spaces:
Paused
Paused
Commit
·
6f6b73c
1
Parent(s):
c50b588
application file
Browse files
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, logging
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
model_name = "microsoft/phi-2"
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(
|
7 |
+
model_name,
|
8 |
+
trust_remote_code=True
|
9 |
+
)
|
10 |
+
model.config.use_cache = False
|
11 |
+
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
13 |
+
tokenizer.pad_token = tokenizer.eos_token
|
14 |
+
|
15 |
+
adapter_path = 'checkpoint-500'
|
16 |
+
model.load_adapter(adapter_path)
|
17 |
+
|
18 |
+
|
19 |
+
def generate_context(prompt, tokens=300):
|
20 |
+
pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_length=tokens)
|
21 |
+
sentence = "[INST] " + prompt + " [/INST]"
|
22 |
+
result = pipe(sentence)
|
23 |
+
text = result[0]['generated_text']
|
24 |
+
|
25 |
+
return text[len(sentence):]
|
26 |
+
|
27 |
+
|
28 |
+
examples = [
|
29 |
+
["What is a large language model?", 200],
|
30 |
+
["Explain the process of photosynthesis", 250]
|
31 |
+
]
|
32 |
+
|
33 |
+
demo = gr.Interface(
|
34 |
+
fn=generate_context,
|
35 |
+
inputs=[
|
36 |
+
gr.Textbox(label="How may I help you ? 🤖"),
|
37 |
+
gr.Slider(200, 500, value=300, label="Sentence length", step=50)
|
38 |
+
],
|
39 |
+
outputs="text",
|
40 |
+
examples=examples
|
41 |
+
)
|
42 |
+
|
43 |
+
demo.launch(debug=True)
|