GJavad commited on
Commit
24a48da
1 Parent(s): 61245a6

Add FastAPI for handling POST requests

Browse files
Files changed (2) hide show
  1. app.py +17 -6
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,7 +1,14 @@
1
  import streamlit as st
2
  from transformers import pipeline
 
 
 
3
 
4
- st.title("Hugging Face Model Demo")
 
 
 
 
5
 
6
 
7
  @st.cache(allow_output_mutation=True)
@@ -11,8 +18,12 @@ def load_model():
11
 
12
  model = load_model()
13
 
14
- user_input = st.text_input("Enter your text:")
15
- if user_input:
16
- with st.spinner("Generating response..."):
17
- result = model(user_input)
18
- st.success(result[0]["generated_text"])
 
 
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ from fastapi import FastAPI
4
+ from pydantic import BaseModel
5
+ import uvicorn
6
 
7
+ app = FastAPI()
8
+
9
+
10
+ class Input(BaseModel):
11
+ input: str
12
 
13
 
14
  @st.cache(allow_output_mutation=True)
 
18
 
19
  model = load_model()
20
 
21
+
22
+ @app.post("/")
23
+ def generate_text(input: Input):
24
+ result = model(input.input)
25
+ return {"generated_text": result[0]["generated_text"]}
26
+
27
+
28
+ if __name__ == "__main__":
29
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt CHANGED
@@ -1,2 +1,4 @@
1
  streamlit
2
- transformers
 
 
 
1
  streamlit
2
+ transformers
3
+ fastapi
4
+ uvicorn