Tonic commited on
Commit
b13baf7
1 Parent(s): b76d73d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer
3
+ from hf_olmo import * # registers the Auto* classes
4
+
5
+
6
+ title = """# 👋🏻Welcome to 🌟Tonic's 👴🏻🏑Olmo
7
+ "[👴🏻🏑allenai/OLMo-7B](https://huggingface.co/allenai/OLMo-7B) is an on-device LLM from Allen-ai that can fit on your laptop ! You can use this demo to try out their model. You can also use [👴🏻🏑allenai/OLMo-7B](https://huggingface.co/allenai/OLMo-7B) [on your laptop & by cloning this space](https://huggingface.co/spaces/Tonic/Olmo/tree/main?clone=true). 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/Tonic/Olmo?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
8
+ Join us : 🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community 👻 [![Join us on Discord](https://img.shields.io/discord/1109943800132010065?label=Discord&logo=discord&style=flat-square)](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Tonic-AI](https://github.com/tonic-ai) & contribute to 🌟 [DataTonic](https://github.com/Tonic-AI/DataTonic) 🤗Big thanks to Yuvi Sharma and all the folks at huggingface for the community grant 🤗
9
+ """
10
+
11
+ model_name = "allenai/OLMo-7B"
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16)
14
+
15
+
16
+ def generate_text(prompt, max_new_tokens, top_k, top_p, do_sample):
17
+ inputs = tokenizer(prompt, return_tensors='pt', return_token_type_ids=False)
18
+ response = model.generate(**inputs, max_new_tokens=max_new_tokens, do_sample=do_sample, top_k=top_k, top_p=top_p)
19
+ return tokenizer.batch_decode(response, skip_special_tokens=True)[0]
20
+
21
+ def main():
22
+ with gr.Blocks() as demo:
23
+ gr.Markdown(title)
24
+ output = gr.Textbox(label="Generated Text", lines=10)
25
+ submit_button = gr.Button("Generate")
26
+ with gr.Accordion("Optional Parameters"):
27
+ max_new_tokens = gr.Slider(minimum=1, maximum=650, step=1, value=300, label="new tokens in reply")
28
+ top_k = gr.Slider(minimum=1, maximum=100, step=1, value=50, label="Top K")
29
+ top_p = gr.Slider(minimum=0.1, maximum=1.0, step=0.01, value=0.95, label="Top P")
30
+ do_sample = gr.Checkbox(value=True, label="Untick for faster inference")
31
+ prompt = gr.Textbox(label="Enter your prompt")
32
+ prompt.change(fn=generate_text, inputs=[prompt, max_new_tokens, top_k, top_p, do_sample], outputs=output, submit=submit_button)
33
+
34
+ demo.launch()
35
+
36
+ if __name__ == "__main__":
37
+ main()