cnmoro commited on
Commit
929430b
·
verified ·
1 Parent(s): b62050e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ from transformers import StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
5
+ from threading import Thread
6
+
7
+ # Loading the tokenizer and model from Hugging Face's model hub.
8
+ tokenizer = AutoTokenizer.from_pretrained("cnmoro/jack-68m-text-structurization")
9
+ model = AutoModelForCausalLM.from_pretrained("cnmoro/jack-68m-text-structurization")
10
+
11
+ # using CUDA for an optimal experience
12
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
13
+ model = model.to(device)
14
+
15
+ # Function to generate model predictions.
16
+ def predict(message, history):
17
+
18
+ model_inputs = tokenizer([
19
+ f"### Structurize: {message}\n\n### Response:\n"
20
+ ], return_tensors="pt").to(device)
21
+
22
+ streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
23
+
24
+ generate_kwargs = dict(
25
+ model_inputs,
26
+ streamer=streamer,
27
+ max_new_tokens=512,
28
+ top_p=0.2,
29
+ top_k=20,
30
+ temperature=0.1,
31
+ num_beams=1
32
+ )
33
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
34
+ t.start() # Starting the generation in a separate thread.
35
+ partial_message = ""
36
+ for new_token in streamer:
37
+ partial_message += new_token
38
+ if '</s>' in partial_message: # Breaking the loop if the stop token is generated.
39
+ break
40
+ yield partial_message
41
+
42
+ # Setting up the Gradio chat interface.
43
+ gr.ChatInterface(predict,
44
+ title="TextStructurization_Jack68m_CPU",
45
+ description="Pass a text to be structurized"
46
+ ).launch() # Launching the web interface.