Spaces:
Running
Running
File size: 11,795 Bytes
d15392d 6c0215b 8b03c54 812d4f6 6c0215b 042861c c11a3a8 6c0215b fd64d36 6c0215b ef5f658 8b03c54 6e95583 8b03c54 6e95583 ef5f658 8b03c54 6c0215b ef5f658 6e95583 6c0215b 6e95583 6c0215b 6e95583 6c0215b d15392d effce56 d15392d e9f3a9a d15392d 39eaf4a c945e1b d5939d1 d15392d 1b2e268 d15392d d61b7ff d15392d d61b7ff d15392d d61b7ff d15392d d61b7ff d15392d d61b7ff d15392d d61b7ff e9f3a9a a0b62ab e9f3a9a ad67d60 e9f3a9a d61b7ff ad10382 d61b7ff d15392d 04f3b88 d15392d d61b7ff d15392d d61b7ff d15392d d61b7ff d15392d d61b7ff d15392d d61b7ff d15392d e9f3a9a a0b62ab e237568 ad67d60 f138f18 e237568 f138f18 ad67d60 e237568 ad67d60 e9f3a9a f138f18 d61b7ff ad67d60 d61b7ff e9f3a9a ad67d60 d61b7ff 39eaf4a d15392d d61b7ff 39eaf4a e9f3a9a a0b62ab 39eaf4a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
'''
from fastapi import FastAPI, Query
from pydantic import BaseModel
import cloudscraper
from bs4 import BeautifulSoup
from transformers import pipeline
import torch
import re
import os
#os.environ["HF_HOME"] = "/home/user/huggingface"
#os.environ["TRANSFORMERS_CACHE"] = "/home/user/huggingface"
app = FastAPI()
class ThreadResponse(BaseModel):
question: str
replies: list[str]
def clean_text(text: str) -> str:
text = text.strip()
text = re.sub(r"\b\d+\s*likes?,?\s*\d*\s*replies?$", "", text, flags=re.IGNORECASE).strip()
return text
@app.get("/scrape", response_model=ThreadResponse)
def scrape(url: str = Query(...)):
scraper = cloudscraper.create_scraper()
response = scraper.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
comment_containers = soup.find_all('div', class_='post__content')
if comment_containers:
question = clean_text(comment_containers[0].get_text(strip=True, separator="\n"))
replies = [clean_text(comment.get_text(strip=True, separator="\n")) for comment in comment_containers[1:]]
return ThreadResponse(question=question, replies=replies)
return ThreadResponse(question="", replies=[])
MODEL_NAME = "microsoft/phi-2"
# Load the text-generation pipeline once at startup
text_generator = pipeline(
"text-generation",
model=MODEL_NAME,
trust_remote_code=True,
device=0 if torch.cuda.is_available() else -1, # GPU if available, else CPU
)
class PromptRequest(BaseModel):
prompt: str
@app.post("/generate")
async def generate_text(request: PromptRequest):
# The model expects a string prompt, so pass request.prompt directly
outputs = text_generator(
request.prompt,
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
do_sample=True,
num_return_sequences=1,
)
generated_text = outputs[0]['generated_text']
# Optional: parse reasoning and content if your model uses special tags like </think>
if "</think>" in generated_text:
reasoning_content = generated_text.split("</think>")[0].strip()
content = generated_text.split("</think>")[1].strip()
else:
reasoning_content = ""
content = generated_text.strip()
return {
"reasoning_content": reasoning_content,
"generated_text": content
}
'''
from fastapi import FastAPI, Query, Path
from pydantic import BaseModel
import cloudscraper
from bs4 import BeautifulSoup
from transformers import AutoTokenizer, AutoModelForCausalLM, T5Tokenizer, T5ForConditionalGeneration, PegasusTokenizer, PegasusForConditionalGeneration
import torch
import re
from fastapi.responses import JSONResponse
from fastapi.requests import Request
from fastapi import status
from typing import List, Dict, Optional
from llama_cpp import Llama
app = FastAPI()
# --- Data Models ---
class ThreadResponse(BaseModel):
question: str
replies: list[str]
class PromptRequest(BaseModel):
prompt: str
class GenerateResponse(BaseModel):
reasoning_content: str
generated_text: str
# New model for summarization request
class SummarizeRequest(BaseModel):
replies: List[str]
task: str # expecting "summarisation"
# New model for summarization response
class SummarizeResponse(BaseModel):
individual_summaries: Dict[int, Dict[str, str]] # {index: {"reasoning": str, "summary": str}}
combined_reasoning: str
combined_summary: str
# --- Utility Functions ---
def clean_text(text: str) -> str:
text = text.strip()
text = re.sub(r"\b\d+\s*likes?,?\s*\d*\s*replies?$", "", text, flags=re.IGNORECASE).strip()
return text
# --- Scraping Endpoint ---
@app.get("/scrape", response_model=ThreadResponse)
def scrape(url: str):
scraper = cloudscraper.create_scraper()
response = scraper.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.content, "html.parser")
comment_containers = soup.find_all("div", class_="post__content")
if comment_containers:
question = clean_text(comment_containers[0].get_text(strip=True, separator="\n"))
replies = [clean_text(comment.get_text(strip=True, separator="\n")) for comment in comment_containers[1:]]
return ThreadResponse(question=question, replies=replies)
return ThreadResponse(question="", replies=[])
# --- Load DeepSeek-R1-Distill-Qwen-1.5B Model & Tokenizer ---
deepseek_model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"
deepseek_tokenizer = AutoTokenizer.from_pretrained(deepseek_model_name)
deepseek_model = AutoModelForCausalLM.from_pretrained(deepseek_model_name)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
deepseek_model = deepseek_model.to(device)
# --- Load T5-Large Model & Tokenizer ---
t5_model_name = "google-t5/t5-large"
t5_tokenizer = T5Tokenizer.from_pretrained(t5_model_name)
t5_model = T5ForConditionalGeneration.from_pretrained(t5_model_name)
t5_model = t5_model.to(device)
pegasus_model_name = "google/pegasus-large"
pegasus_tokenizer = PegasusTokenizer.from_pretrained(pegasus_model_name)
pegasus_model = PegasusForConditionalGeneration.from_pretrained(pegasus_model_name)
pegasus_model = pegasus_model.to(device)
qwen3_model_name = "Qwen/Qwen3-0.6B"
qwen3_tokenizer = AutoTokenizer.from_pretrained(qwen3_model_name)
qwen3_model = AutoModelForCausalLM.from_pretrained(qwen3_model_name)
qwen3_model = qwen3_model.to(device)
qwen3_gguf_llm = Llama.from_pretrained(
repo_id="unsloth/Qwen3-0.6B-GGUF",
filename="Qwen3-0.6B-BF16.gguf",
)
# --- Generation Functions ---
def generate_deepseek(prompt: str) -> (str, str):
inputs = deepseek_tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(device)
outputs = deepseek_model.generate(
**inputs,
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
do_sample=True,
num_return_sequences=1,
pad_token_id=deepseek_tokenizer.eos_token_id,
)
generated_text = deepseek_tokenizer.decode(outputs[0], skip_special_tokens=True)
if "</think>" in generated_text:
reasoning_content, content = generated_text.split("</think>", 1)
return reasoning_content.strip(), content.strip()
else:
return "", generated_text.strip()
def generate_t5(prompt: str) -> (str, str):
inputs = t5_tokenizer.encode(prompt, return_tensors="pt", max_length=512, truncation=True).to(device)
outputs = t5_model.generate(
inputs,
max_length=512,
num_beams=4,
repetition_penalty=2.5,
length_penalty=1.0,
early_stopping=True,
)
generated_text = t5_tokenizer.decode(outputs[0], skip_special_tokens=True)
if "</think>" in generated_text:
reasoning_content, content = generated_text.split("</think>", 1)
return reasoning_content.strip(), content.strip()
else:
return "", generated_text.strip()
# --- API Endpoints ---
def generate_pegasus(prompt: str) -> (str, str):
# Pegasus expects raw text input (no prefix needed)
inputs = pegasus_tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=1024,
).to(device)
outputs = pegasus_model.generate(
**inputs,
max_new_tokens=150,
num_beams=4,
length_penalty=2.0,
early_stopping=True,
)
generated_text = pegasus_tokenizer.decode(outputs[0], skip_special_tokens=True)
# Pegasus does not use <think> tags, so no reasoning extraction
return "", generated_text.strip()
def generate_qwen3(prompt: str) -> (str, str):
inputs = qwen3_tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=1024,
).to(device)
outputs = qwen3_model.generate(
**inputs,
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
do_sample=True,
num_return_sequences=1,
pad_token_id=qwen3_tokenizer.eos_token_id,
)
generated_text = qwen3_tokenizer.decode(outputs[0], skip_special_tokens=True)
if "</think>" in generated_text:
reasoning_content, content = generated_text.split("</think>", 1)
return reasoning_content.strip(), content.strip()
else:
return "", generated_text.strip()
def generate_qwen3_gguf(prompt: str, max_tokens: int = 256) -> (str, str):
messages = [
{"role": "user", "content": prompt}
]
response = qwen3_gguf_llm.create_chat_completion(
messages=messages,
max_tokens=max_tokens,
)
generated_text = response['choices'][0]['message']['content']
if "</think>" in generated_text:
reasoning_content, content = generated_text.split("</think>", 1)
return reasoning_content.strip() + "</think>", content.strip()
else:
return "", generated_text.strip()
# --- New summarization endpoint ---
@app.post("/summarize_thread", response_model=SummarizeResponse)
async def summarize_thread(request: SummarizeRequest):
if request.task.lower() != "summarisation":
return JSONResponse(
status_code=400,
content={"error": "Unsupported task. Only 'summarisation' is supported."}
)
individual_summaries = {}
combined_reasonings = []
combined_summaries = []
# Summarize each reply individually
for idx, reply in enumerate(request.replies):
reasoning, summary = generate_qwen3_gguf(reply, max_tokens=256)
individual_summaries[idx] = {
"reasoning": reasoning,
"summary": summary
}
if reasoning:
combined_reasonings.append(reasoning)
combined_summaries.append(summary)
# Combine all individual summaries into one text
combined_summary_text = " ".join(combined_summaries)
# Recursively summarize combined summary if too long (optional)
# Here, we summarize combined summary to get final reasoning and summary
final_reasoning, final_summary = generate_qwen3_gguf(combined_summary_text, max_tokens=256)
# Append final reasoning to combined reasonings
if final_reasoning:
combined_reasonings.append(final_reasoning)
return SummarizeResponse(
individual_summaries=individual_summaries,
combined_reasoning="\n\n".join(combined_reasonings).strip(),
combined_summary=final_summary.strip()
)
@app.post("/generate/{model_name}", response_model=GenerateResponse)
async def generate(
request: PromptRequest,
model_name: str = Path(..., description="Model to use: 'deepseekr1-qwen', 't5-large', 'pegasus-large', 'qwen3-0.6b-hf', or 'qwen3-0.6b-gguf'")
):
if model_name == "deepseekr1-qwen":
reasoning, text = generate_deepseek(request.prompt)
elif model_name == "t5-large":
reasoning, text = generate_t5(request.prompt)
elif model_name == "pegasus-large":
reasoning, text = generate_pegasus(request.prompt)
elif model_name == "qwen3-0.6b-hf":
reasoning, text = generate_qwen3_hf(request.prompt)
elif model_name == "qwen3-0.6b-gguf":
reasoning, text = generate_qwen3_gguf(request.prompt)
else:
return GenerateResponse(reasoning_content="", generated_text=f"Error: Unknown model '{model_name}'.")
return GenerateResponse(reasoning_content=reasoning, generated_text=text)
# --- Global Exception Handler ---
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
print(f"Exception: {exc}")
return JSONResponse(
status_code=status.HTTP_200_OK,
content={
"reasoning_content": "",
"generated_text": f"Error: {str(exc)}"
}
)
|