ldhldh commited on
Commit
a6d3fd5
·
verified ·
1 Parent(s): 6667ce7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -10
app.py CHANGED
@@ -1,18 +1,47 @@
1
  import gradio
2
 
3
- def my_inference_function(name):
4
- return "Hello " + name + "!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  gradio_interface = gradio.Interface(
7
- fn=my_inference_function,
8
- inputs="text",
9
- outputs="text",
10
- examples=[
11
  ["Jill"],
12
  ["Sam"]
13
- ],
14
- title="REST API with Gradio and Huggingface Spaces",
15
- description="This is a demo of how to build an AI powered REST API with Gradio and Huggingface Spaces – for free! Based on [this article](https://www.tomsoderlund.com/ai/building-ai-powered-rest-api). See the **Use via API** link at the bottom of this page.",
16
- article="© Tom Söderlund 2022"
17
  )
18
  gradio_interface.launch()
 
1
  import gradio
2
 
3
+ import torch
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+
6
+
7
+ MODEL_NAME = "arnir0/Tiny-LLM"
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
10
+ model = AutoModelForCausalLM.from_pretrained(MODEL_NAME)
11
+
12
+ def generate_text(prompt, model, tokenizer, max_length=4096, temperature=0.8, top_k=50, top_p=0.95):
13
+ inputs = tokenizer.encode(prompt, return_tensors="pt")
14
+
15
+ outputs = model.generate(
16
+ inputs,
17
+ max_length=max_length,
18
+ temperature=temperature,
19
+ top_k=top_k,
20
+ top_p=top_p,
21
+ do_sample=True
22
+ )
23
+
24
+
25
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
26
+ return generated_text
27
+
28
+
29
+ def my_inference_function(text):
30
+ prompt = f"Summary the context below\n\n{text}"
31
+ generated_text = generate_text(prompt, model, tokenizer)
32
+
33
+ return generated_text[len(prompt):]
34
 
35
  gradio_interface = gradio.Interface(
36
+ fn=my_inference_function,
37
+ inputs="text",
38
+ outputs="text",
39
+ examples=[
40
  ["Jill"],
41
  ["Sam"]
42
+ ],
43
+ title="REST API with Gradio and Huggingface Spaces",
44
+ description="This is a demo of how to build an AI powered REST API with Gradio and Huggingface Spaces – for free! Based on [this article](https://www.tomsoderlund.com/ai/building-ai-powered-rest-api). See the **Use via API** link at the bottom of this page.",
45
+ article="© Tom Söderlund 2022"
46
  )
47
  gradio_interface.launch()