Danil commited on
Commit
7f3e850
β€’
1 Parent(s): dc685c3
Files changed (3) hide show
  1. app.py +10 -19
  2. requirements.txt +4 -2
  3. server.py +16 -0
app.py CHANGED
@@ -1,16 +1,11 @@
1
  import streamlit as st
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
-
4
- @st.cache(allow_output_mutation=True)
5
- def load_model():
6
- '''
7
- Loads the model and tokenizer from the local directory.
8
- :return: A list containing the model and the tokenizer.
9
- '''
10
- model_name = 'facebook/incoder-1B'
11
- tokenizer = AutoTokenizer.from_pretrained(model_name)
12
- model = AutoModelForCausalLM.from_pretrained(model_name, low_cpu_mem_usage=True)
13
- return [model, tokenizer]
14
 
15
  st.set_page_config(
16
  page_title="Incoder Example",
@@ -37,11 +32,7 @@ repetition_penalty = st.slider('repetition_penalty', 1.0, 10.0, 1.0)
37
 
38
  if gen:
39
  c.code('Generating...')
40
- m = load_model()
41
-
42
- inpt = m[1].encode(txt, return_tensors="pt")
43
- out = m[0].generate(inpt, max_length=max_length, top_p=top_p, top_k=top_k, temperature=temperature, num_beams=num_beams, repetition_penalty=repetition_penalty)
44
- res = m[1].decode(out[0])
45
-
46
  print('ok')
47
- c.code(res)
 
1
  import streamlit as st
2
+ import requests
3
+ import os
4
+ import json
5
+
6
+ def start_server():
7
+ os.system("uvicorn server:app --port 8080 --host 0.0.0.0 --workers 2")
8
+ st.session_state['server_started'] = True
 
 
 
 
 
9
 
10
  st.set_page_config(
11
  page_title="Incoder Example",
 
32
 
33
  if gen:
34
  c.code('Generating...')
35
+ req = f"http://0.0.0.0:8080?input_text={txt}&top_p={top_p}&top_k={top_k}&temperature={temperature}&num_beams={num_beams}&repetition_penalty={repetition_penalty}&max_length={max_length}"
36
+ res = requests.get(req)
 
 
 
 
37
  print('ok')
38
+ c.code(json.loads(res.text))
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  transformers
2
- streamlit
3
- torch
 
 
 
1
  transformers
2
+ fastapi
3
+ uvicorn
4
+ torch
5
+ requests
server.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uvicorn
2
+ from fastapi import FastAPI
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ app = FastAPI()
6
+
7
+ model_name = 'facebook/incoder-1B'
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+ model = AutoModelForCausalLM.from_pretrained(model_name, low_cpu_mem_usage=True)
10
+
11
+ @app.get("/")
12
+ def read_root(input_text, max_length, top_p, top_k, num_beams, temperature, repetition_penalty):
13
+ inpt = model_name.encode(input_text, return_tensors="pt")
14
+ out = model.generate(inpt, max_length=max_length, top_p=top_p, top_k=top_k, temperature=temperature, num_beams=num_beams, repetition_penalty=repetition_penalty)
15
+ res = tokenizer.decode(out[0])
16
+ return {res}