Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -86,6 +86,9 @@ from bs4 import BeautifulSoup
|
|
86 |
from transformers import AutoTokenizer, AutoModelForCausalLM, T5Tokenizer, T5ForConditionalGeneration
|
87 |
import torch
|
88 |
import re
|
|
|
|
|
|
|
89 |
|
90 |
app = FastAPI()
|
91 |
|
@@ -102,7 +105,6 @@ class GenerateResponse(BaseModel):
|
|
102 |
reasoning_content: str
|
103 |
generated_text: str
|
104 |
|
105 |
-
|
106 |
# --- Utility Functions ---
|
107 |
|
108 |
def clean_text(text: str) -> str:
|
@@ -110,7 +112,6 @@ def clean_text(text: str) -> str:
|
|
110 |
text = re.sub(r"\b\d+\s*likes?,?\s*\d*\s*replies?$", "", text, flags=re.IGNORECASE).strip()
|
111 |
return text
|
112 |
|
113 |
-
|
114 |
# --- Scraping Endpoint ---
|
115 |
|
116 |
@app.get("/scrape", response_model=ThreadResponse)
|
@@ -128,7 +129,6 @@ def scrape(url: str):
|
|
128 |
return ThreadResponse(question=question, replies=replies)
|
129 |
return ThreadResponse(question="", replies=[])
|
130 |
|
131 |
-
|
132 |
# --- Load DeepSeek-R1-Distill-Qwen-1.5B Model & Tokenizer ---
|
133 |
|
134 |
deepseek_model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
@@ -137,7 +137,6 @@ deepseek_model = AutoModelForCausalLM.from_pretrained(deepseek_model_name)
|
|
137 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
138 |
deepseek_model = deepseek_model.to(device)
|
139 |
|
140 |
-
|
141 |
# --- Load T5-Large Model & Tokenizer ---
|
142 |
|
143 |
t5_model_name = "google-t5/t5-large"
|
@@ -145,7 +144,6 @@ t5_tokenizer = T5Tokenizer.from_pretrained(t5_model_name)
|
|
145 |
t5_model = T5ForConditionalGeneration.from_pretrained(t5_model_name)
|
146 |
t5_model = t5_model.to(device)
|
147 |
|
148 |
-
|
149 |
# --- Generation Functions ---
|
150 |
|
151 |
def generate_deepseek(prompt: str) -> (str, str):
|
@@ -167,9 +165,7 @@ def generate_deepseek(prompt: str) -> (str, str):
|
|
167 |
else:
|
168 |
return "", generated_text.strip()
|
169 |
|
170 |
-
|
171 |
def generate_t5(prompt: str) -> (str, str):
|
172 |
-
# T5 expects prompt with task prefix, e.g. "summarize: ..."
|
173 |
inputs = t5_tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True).to(device)
|
174 |
outputs = t5_model.generate(
|
175 |
inputs,
|
@@ -181,14 +177,12 @@ def generate_t5(prompt: str) -> (str, str):
|
|
181 |
)
|
182 |
generated_text = t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
183 |
|
184 |
-
# Optional reasoning parsing if </think> is used
|
185 |
if "</think>" in generated_text:
|
186 |
reasoning_content, content = generated_text.split("</think>", 1)
|
187 |
return reasoning_content.strip(), content.strip()
|
188 |
else:
|
189 |
return "", generated_text.strip()
|
190 |
|
191 |
-
|
192 |
# --- API Endpoints ---
|
193 |
|
194 |
@app.post("/generate/{model_name}", response_model=GenerateResponse)
|
@@ -201,6 +195,19 @@ async def generate(
|
|
201 |
elif model_name == "t5-large":
|
202 |
reasoning, text = generate_t5(request.prompt)
|
203 |
else:
|
204 |
-
return
|
205 |
|
206 |
return GenerateResponse(reasoning_content=reasoning, generated_text=text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
from transformers import AutoTokenizer, AutoModelForCausalLM, T5Tokenizer, T5ForConditionalGeneration
|
87 |
import torch
|
88 |
import re
|
89 |
+
from fastapi.responses import JSONResponse
|
90 |
+
from fastapi.requests import Request
|
91 |
+
from fastapi import status
|
92 |
|
93 |
app = FastAPI()
|
94 |
|
|
|
105 |
reasoning_content: str
|
106 |
generated_text: str
|
107 |
|
|
|
108 |
# --- Utility Functions ---
|
109 |
|
110 |
def clean_text(text: str) -> str:
|
|
|
112 |
text = re.sub(r"\b\d+\s*likes?,?\s*\d*\s*replies?$", "", text, flags=re.IGNORECASE).strip()
|
113 |
return text
|
114 |
|
|
|
115 |
# --- Scraping Endpoint ---
|
116 |
|
117 |
@app.get("/scrape", response_model=ThreadResponse)
|
|
|
129 |
return ThreadResponse(question=question, replies=replies)
|
130 |
return ThreadResponse(question="", replies=[])
|
131 |
|
|
|
132 |
# --- Load DeepSeek-R1-Distill-Qwen-1.5B Model & Tokenizer ---
|
133 |
|
134 |
deepseek_model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
|
|
|
137 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
138 |
deepseek_model = deepseek_model.to(device)
|
139 |
|
|
|
140 |
# --- Load T5-Large Model & Tokenizer ---
|
141 |
|
142 |
t5_model_name = "google-t5/t5-large"
|
|
|
144 |
t5_model = T5ForConditionalGeneration.from_pretrained(t5_model_name)
|
145 |
t5_model = t5_model.to(device)
|
146 |
|
|
|
147 |
# --- Generation Functions ---
|
148 |
|
149 |
def generate_deepseek(prompt: str) -> (str, str):
|
|
|
165 |
else:
|
166 |
return "", generated_text.strip()
|
167 |
|
|
|
168 |
def generate_t5(prompt: str) -> (str, str):
|
|
|
169 |
inputs = t5_tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True).to(device)
|
170 |
outputs = t5_model.generate(
|
171 |
inputs,
|
|
|
177 |
)
|
178 |
generated_text = t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
179 |
|
|
|
180 |
if "</think>" in generated_text:
|
181 |
reasoning_content, content = generated_text.split("</think>", 1)
|
182 |
return reasoning_content.strip(), content.strip()
|
183 |
else:
|
184 |
return "", generated_text.strip()
|
185 |
|
|
|
186 |
# --- API Endpoints ---
|
187 |
|
188 |
@app.post("/generate/{model_name}", response_model=GenerateResponse)
|
|
|
195 |
elif model_name == "t5-large":
|
196 |
reasoning, text = generate_t5(request.prompt)
|
197 |
else:
|
198 |
+
return GenerateResponse(reasoning_content="", generated_text=f"Error: Unknown model '{model_name}'.")
|
199 |
|
200 |
return GenerateResponse(reasoning_content=reasoning, generated_text=text)
|
201 |
+
|
202 |
+
# --- Global Exception Handler ---
|
203 |
+
|
204 |
+
@app.exception_handler(Exception)
|
205 |
+
async def global_exception_handler(request: Request, exc: Exception):
|
206 |
+
print(f"Exception: {exc}")
|
207 |
+
return JSONResponse(
|
208 |
+
status_code=status.HTTP_200_OK,
|
209 |
+
content={
|
210 |
+
"reasoning_content": "",
|
211 |
+
"generated_text": f"Error: {str(exc)}"
|
212 |
+
}
|
213 |
+
)
|