Update main.py
Browse files
main.py
CHANGED
@@ -1,17 +1,16 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
|
|
3 |
import torch
|
4 |
-
from transformers import pipeline
|
5 |
from fastapi.middleware.cors import CORSMiddleware
|
6 |
-
import os
|
7 |
-
from huggingface_hub import login
|
8 |
|
9 |
-
|
10 |
-
|
|
|
11 |
print(access_token_read)
|
12 |
|
13 |
-
|
14 |
-
login(token=access_token_read)
|
15 |
|
16 |
# Define the FastAPI app
|
17 |
app = FastAPI()
|
@@ -23,21 +22,17 @@ app.add_middleware(
|
|
23 |
allow_headers=["*"],
|
24 |
)
|
25 |
|
|
|
26 |
|
27 |
|
28 |
|
29 |
-
|
30 |
-
pipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="auto")
|
31 |
-
|
32 |
-
|
33 |
# Define the request model for email input
|
34 |
class EmailRequest(BaseModel):
|
35 |
subject: str
|
36 |
sender: str
|
37 |
recipients: str
|
38 |
body: str
|
39 |
-
|
40 |
-
|
41 |
def create_email_prompt(subject, sender, recipients, body):
|
42 |
messages = [
|
43 |
{
|
@@ -57,7 +52,7 @@ def create_email_prompt(subject, sender, recipients, body):
|
|
57 |
Body:
|
58 |
{body}
|
59 |
|
60 |
-
|
61 |
"""
|
62 |
}
|
63 |
]
|
@@ -65,17 +60,12 @@ def create_email_prompt(subject, sender, recipients, body):
|
|
65 |
return prompt
|
66 |
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
# Define the FastAPI endpoint for email summarization
|
73 |
@app.post("/summarize-email/")
|
74 |
async def summarize_email(email: EmailRequest):
|
75 |
prompt = create_email_prompt(email.subject, email.sender, email.recipients, email.body)
|
76 |
|
77 |
# Use the pipeline to generate the summary
|
78 |
-
|
79 |
|
80 |
-
return {"summary":
|
81 |
-
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
2 |
from pydantic import BaseModel
|
3 |
+
import transformers
|
4 |
import torch
|
|
|
5 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
6 |
|
7 |
+
|
8 |
+
import os
|
9 |
+
access_token_read = os.getenv("DS4")
|
10 |
print(access_token_read)
|
11 |
|
12 |
+
from huggingface_hub import login
|
13 |
+
login(token = access_token_read)
|
14 |
|
15 |
# Define the FastAPI app
|
16 |
app = FastAPI()
|
|
|
22 |
allow_headers=["*"],
|
23 |
)
|
24 |
|
25 |
+
pipe = transformers.pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="auto")
|
26 |
|
27 |
|
28 |
|
|
|
|
|
|
|
|
|
29 |
# Define the request model for email input
|
30 |
class EmailRequest(BaseModel):
|
31 |
subject: str
|
32 |
sender: str
|
33 |
recipients: str
|
34 |
body: str
|
35 |
+
|
|
|
36 |
def create_email_prompt(subject, sender, recipients, body):
|
37 |
messages = [
|
38 |
{
|
|
|
52 |
Body:
|
53 |
{body}
|
54 |
|
55 |
+
Provide a concise summary of email body in points that includes important information, if any actions are required, and the priority of the email.
|
56 |
"""
|
57 |
}
|
58 |
]
|
|
|
60 |
return prompt
|
61 |
|
62 |
|
|
|
|
|
|
|
|
|
63 |
# Define the FastAPI endpoint for email summarization
|
64 |
@app.post("/summarize-email/")
|
65 |
async def summarize_email(email: EmailRequest):
|
66 |
prompt = create_email_prompt(email.subject, email.sender, email.recipients, email.body)
|
67 |
|
68 |
# Use the pipeline to generate the summary
|
69 |
+
outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
|
70 |
|
71 |
+
return {"summary": outputs}
|
|