alpeshsonar commited on
Commit
7065e35
·
verified ·
1 Parent(s): 95ac22e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -25
app.py CHANGED
@@ -1,32 +1,15 @@
1
  import gradio as gr
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
4
- from fastapi import FastAPI
5
  from pydantic import BaseModel
6
- from threading import Thread
7
- import uvicorn
8
 
9
  # Initialize FastAPI and Gradio
10
- app = FastAPI()
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
 
13
  # Load the tokenizer and model once for use in both FastAPI and Gradio
14
  tokenizer = T5Tokenizer.from_pretrained("alpeshsonar/lot-t5-small-filter", legacy=False)
15
  model = T5ForConditionalGeneration.from_pretrained("alpeshsonar/lot-t5-small-filter").to(device)
16
 
17
- # Health check endpoint
18
- @app.get("/health")
19
- async def health_check():
20
- return {"status": "API is running"}
21
-
22
- # FastAPI endpoint
23
- @app.post("/generate")
24
- async def generate_text_api(input_text: str):
25
- inputs = tokenizer.encode("Extract lots from given text.\n" + input_text, return_tensors="pt").to(device)
26
- outputs = model.generate(inputs, max_new_tokens=1024)
27
- result = tokenizer.decode(outputs[0], skip_special_tokens=True)
28
- return {"output": result}
29
-
30
  # Gradio interface
31
  def generate_text(input_text):
32
  inputs = tokenizer.encode("Extract lots from given text.\n" + input_text, return_tensors="pt").to(device)
@@ -38,15 +21,8 @@ iface = gr.Interface(fn=generate_text, inputs="text", outputs="text", title="Lin
38
 
39
  # Function to run both FastAPI and Gradio
40
  def run():
41
- # Start FastAPI in a separate thread
42
- def start_fastapi():
43
- uvicorn.run(app, host="0.0.0.0", port=7860)
44
-
45
- t = Thread(target=start_fastapi)
46
- t.start()
47
-
48
  # Launch Gradio interface
49
- iface.launch()
50
 
51
  if __name__ == "__main__":
52
  run()
 
1
  import gradio as gr
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
 
4
  from pydantic import BaseModel
 
 
5
 
6
  # Initialize FastAPI and Gradio
 
7
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
 
9
  # Load the tokenizer and model once for use in both FastAPI and Gradio
10
  tokenizer = T5Tokenizer.from_pretrained("alpeshsonar/lot-t5-small-filter", legacy=False)
11
  model = T5ForConditionalGeneration.from_pretrained("alpeshsonar/lot-t5-small-filter").to(device)
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Gradio interface
14
  def generate_text(input_text):
15
  inputs = tokenizer.encode("Extract lots from given text.\n" + input_text, return_tensors="pt").to(device)
 
21
 
22
  # Function to run both FastAPI and Gradio
23
  def run():
 
 
 
 
 
 
 
24
  # Launch Gradio interface
25
+ iface.launch(server_name="0.0.0.0", server_port=7860)
26
 
27
  if __name__ == "__main__":
28
  run()