mrfakename commited on
Commit
a63bb15
1 Parent(s): 97bfed4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
4
+ import gradio as gr
5
+ from threading import Thread
6
+ device = "cpu"
7
+ if torch.cuda.is_available():
8
+ device = "cuda"
9
+ if torch.backends.mps.is_available():
10
+ device = "mps"
11
+
12
+ theme = gr.themes.Base(
13
+ font=[gr.themes.GoogleFont('Libre Franklin'), gr.themes.GoogleFont('Public Sans'), 'system-ui', 'sans-serif'],
14
+ )
15
+
16
+ tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/cosmo-1b", trust_remote_code=True)
17
+ model = AutoModelForCausalLM.from_pretrained(
18
+ "HuggingFaceTB/cosmo-1b",
19
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
20
+ trust_remote_code=True,
21
+ ).to(device)
22
+ @spaces.GPU(enable_queue=True)
23
+ def generate_text(text, temperature, maxLen):
24
+ inputs = tokenizer([text], return_tensors="pt").to(device)
25
+ streamer = TextIteratorStreamer(tokenizer)
26
+ generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=maxLen, temperature=temperature)
27
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
28
+ thread.start()
29
+ t = ""
30
+ toks = 0
31
+ for out in streamer:
32
+ t += out
33
+ yield t
34
+ with gr.Blocks(theme=theme) as demo:
35
+ gr.Markdown("""
36
+ # (Unofficial) Demo of Hugging Face's Cosmo 1B
37
+
38
+ The model is suitable for commercial use and is licensed under the Apache license. I am not responsible for any outputs you generate. You are solely responsible for ensuring that your usage of the model complies with applicable laws and regulations.
39
+
40
+ I am not affiliated with the authors of the model (Hugging Face).
41
+
42
+ Note: for longer generation (>512), keep clicking "Generate!" The demo is currently limited to 512 demos per generation to ensure all users have access to this service. Please note that once you start generating, you cannot stop generating until the generation is done.
43
+
44
+ By [mrfakename](https://twitter.com/realmrfakename).
45
+
46
+ Duplicate this Space to skip the wait!
47
+ """.strip())
48
+ gr.DuplicateButton()
49
+ text = gr.Textbox(label="Prompt", lines=10, interactive=True, placeholder="Write a detailed analogy between mathematics and a lighthouse.")
50
+ temp = gr.Slider(label="Temperature", minimum=0.1, maximum=1.5, value=0.7)
51
+ maxlen = gr.Slider(label="Max Length", minimum=4, maximum=512, value=75)
52
+ go = gr.Button("Generate", variant="primary")
53
+ go.click(generate_text, inputs=[text, temp, maxlen], outputs=[text], concurrency_limit=2)
54
+ examples = gr.Examples(
55
+ [
56
+ ['Write a detailed analogy between mathematics and a lighthouse.', 0.7, 75],
57
+ ['[INST] Generate a story involving a dog, an astronaut and a baker [/INST]', 0.7, 75],
58
+ ['''def print_prime(n):
59
+ """
60
+ Print all primes between 1 and n
61
+ """\n''', 0.2, 100],
62
+ ],
63
+ [text, temp, maxlen]
64
+ )
65
+
66
+ if __name__ == "__main__":
67
+ demo.queue(api_open=False).launch()
68
+