| from fastapi import FastAPI |
| from pydantic import BaseModel |
| import torch |
| from transformers import pipeline |
| from fastapi.middleware.cors import CORSMiddleware |
| import os |
| from huggingface_hub import login |
|
|
| |
| access_token_read = os.getenv('DS4') |
| print(access_token_read) |
|
|
| |
| login(token=access_token_read) |
|
|
| |
| app = FastAPI() |
|
|
| app.add_middleware( |
| CORSMiddleware, |
| allow_origins=["*"], |
| allow_methods=["*"], |
| allow_headers=["*"], |
| ) |
|
|
|
|
|
|
|
|
|
|
| pipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="auto") |
|
|
|
|
| |
| class EmailRequest(BaseModel): |
| subject: str |
| sender: str |
| recipients: str |
| body: str |
|
|
|
|
| def create_email_prompt(subject, sender, recipients, body): |
| messages = [ |
| { |
| "role": "system", |
| "content": "You are an email summarizer. Your goal is to provide a concise summary by focusing on key points, action items, and urgency." |
| }, |
| { |
| "role": "user", |
| "content": f""" |
| Summarize the following email by focusing on the key points, action items, and urgency. |
| |
| Email Details: |
| Subject: {subject} |
| Sender: {sender} |
| Recipients: {recipients} |
| |
| Body: |
| {body} |
| |
| Provide a concise summary that includes important information, if any actions are required, and the priority of the email. |
| """ |
| } |
| ] |
| prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) |
| return prompt |
|
|
|
|
|
|
|
|
|
|
|
|
| |
| @app.post("/summarize-email/") |
| async def summarize_email(email: EmailRequest): |
| prompt = create_email_prompt(email.subject, email.sender, email.recipients, email.body) |
| |
| |
| summary = pipeline(prompt)[0]["generated_text"] |
| |
| return {"summary": summary} |
|
|
|
|