bkoz commited on
Commit
eaa6aa4
1 Parent(s): b5d0ba6
Files changed (1) hide show
  1. app.py +11 -69
app.py CHANGED
@@ -1,73 +1,15 @@
1
- import gradio as gr
2
  import spaces
3
- # import torch
4
- from huggingface_hub import hf_hub_download
5
- from llama_cpp import Llama, LlamaGrammar
6
 
7
-
8
- # zero = torch.Tensor([0]).cuda()
9
- # print(f'zero.device: {zero.device}') # <-- 'cpu' 🤔
10
 
11
  @spaces.GPU
12
- def greet(n):
13
- global llm
14
- llm = load_model(download_model())
15
-
16
- # print(f'zero.device: {zero.device}') # <-- 'cuda:0' 🤗
17
- grammar = LlamaGrammar.from_string('''
18
- root ::= sentence
19
- answer ::= (weather | complaint | yesno | gen)
20
- weather ::= ("Sunny." | "Cloudy." | "Rainy.")
21
- complaint ::= "I don't like talking about the weather."
22
- yesno ::= ("Yes." | "No.")
23
- gen ::= "1. " [A-Z] [a-z] [a-z]*
24
- sentence ::= [A-Z] [A-Za-z0-9 ,-]* ("." | "!" | "?")
25
- ''')
26
-
27
- prompts = [
28
- "How's the weather in London?",
29
- "How's the weather in Munich?",
30
- "How's the weather in Barcelona?",
31
- ]
32
-
33
- print(f'Making a big inference...... {prompts[0]}')
34
- output = llm(
35
- prompts[0],
36
- max_tokens=512,
37
- temperature=0.4,
38
- grammar=grammar
39
- )
40
- print(f'Returned..... {output}')
41
-
42
- s = output['choices'][0]['text']
43
- print(f'{s} , len(s) = {len(s)}')
44
- print(output['choices'])
45
- print(output['choices'][0]['text'])
46
- print()
47
-
48
- return f"Hello {s} Tensor"
49
-
50
- def download_model():
51
-
52
- REPO_ID = "TheBloke/Llama-2-7B-GGUF"
53
- FILENAME = "llama-2-7b.Q5_K_S.gguf"
54
-
55
- print(f'Downloading model {REPO_ID}/{FILENAME}')
56
- m = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
57
- print(f'status: {m}')
58
- return m
59
-
60
- def load_model(fp):
61
- from llama_cpp import Llama, LlamaGrammar
62
-
63
- print(f'Loading model: {fp}')
64
- model_file=fp
65
- llm = Llama(
66
- model_path=model_file,
67
- n_gpu_layers=-1, verbose=True
68
- )
69
- return llm
70
-
71
- demo = gr.Interface(fn=greet, inputs=gr.Number(), outputs=gr.Text())
72
- demo.launch(share=False)
73
-
 
 
1
  import spaces
2
+ from diffusers import DiffusionPipeline
 
 
3
 
4
+ pipe = DiffusionPipeline.from_pretrained(...)
5
+ pipe.to('cuda')
 
6
 
7
  @spaces.GPU
8
+ def generate(prompt):
9
+ return pipe(prompt).images
10
+
11
+ gr.Interface(
12
+ fn=generate,
13
+ inputs=gr.Text(),
14
+ outputs=gr.Gallery(),
15
+ ).launch()