Ikpia commited on
Commit
1dee604
·
1 Parent(s): bfb03c8
Files changed (3) hide show
  1. Dockerfile +2 -0
  2. app/main.py +78 -1
  3. requirements.txt +1 -1
Dockerfile CHANGED
@@ -9,6 +9,8 @@ COPY ./app ./app
9
  COPY requirements.txt .
10
 
11
  RUN pip install --upgrade pip && pip install -r requirements.txt
 
 
12
 
13
  # ✅ Fix: Use a safe cache directory
14
  ENV TRANSFORMERS_CACHE=/tmp/huggingface
 
9
  COPY requirements.txt .
10
 
11
  RUN pip install --upgrade pip && pip install -r requirements.txt
12
+ #Download spaCy English model
13
+ RUN python -m spacy download en_core_web_sm
14
 
15
  # ✅ Fix: Use a safe cache directory
16
  ENV TRANSFORMERS_CACHE=/tmp/huggingface
app/main.py CHANGED
@@ -1,4 +1,4 @@
1
-
2
  from fastapi import FastAPI
3
  from pydantic import BaseModel
4
  from app.model import model, tokenizer
@@ -48,3 +48,80 @@ If the user message or prompt is too long tell the user that you have received h
48
  @app.get("/")
49
  def read_root():
50
  return {"message": "Welcome to my Hugging Face Space!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ '''
2
  from fastapi import FastAPI
3
  from pydantic import BaseModel
4
  from app.model import model, tokenizer
 
48
  @app.get("/")
49
  def read_root():
50
  return {"message": "Welcome to my Hugging Face Space!"}
51
+ '''
52
+
53
+ from fastapi import FastAPI
54
+ from pydantic import BaseModel
55
+ from app.model import model, tokenizer # Ensure your model and tokenizer are imported from your app
56
+ import torch
57
+ import spacy
58
+
59
+ app = FastAPI()
60
+
61
+ # Load spaCy for name detection
62
+ nlp = spacy.load("en_core_web_sm")
63
+
64
+ class Prompt(BaseModel):
65
+ text: str
66
+
67
+ # Function to extract user's name from message
68
+ def extract_name(text: str) -> str:
69
+ doc = nlp(text)
70
+ for ent in doc.ents:
71
+ if ent.label_ == "PERSON":
72
+ return ent.text
73
+ lowered = text.lower()
74
+ if "my name is" in lowered:
75
+ return text.split("my name is")[-1].split(".")[0].strip().split()[0].capitalize()
76
+ elif "best," in lowered or "thanks," in lowered:
77
+ return text.strip().split()[-1].capitalize()
78
+ return ""
79
+
80
+ @app.post("/generate")
81
+ def generate(prompt: Prompt):
82
+ user_input = prompt.text.strip()
83
+ user_name = extract_name(user_input)
84
+
85
+ # Format the full prompt for the model
86
+ prompt_template = f"""
87
+ You are a professional human email assistant working for a company.
88
+
89
+ Your goal is to reply to user messages with helpful, professional, and clearly written email replies.
90
+
91
+ Follow these rules:
92
+ - NEVER mention you're an AI or a model.
93
+ - Use complete, natural, and formal English — sound like a real assistant.
94
+ - If the message includes a name (e.g., “My name is Grace” or ends with “Best, John”), politely address the person by that name.
95
+ - Be brief and respectful if the request is unclear or general, e.g., "Could you please clarify your request?"
96
+ - If the message contains specific details like claim numbers or appointment requests, acknowledge receipt and indicate further action will be taken.
97
+ - If the message is too long or complex, thank the user and say you'll get back to them soon, addressing them by name if provided.
98
+ - Always end with a polite closing, like "Best regards" or "Sincerely", without using placeholder names.
99
+
100
+ Here is the user's message:
101
+ \"\"\"{user_input}\"\"\"
102
+
103
+ Reply with a professional email below. Do not include explanations, examples, or placeholders.
104
+ """
105
+ full_prompt = f"<s>[INST] {prompt_template.strip()} [/INST]"
106
+
107
+ device = "cuda" if torch.cuda.is_available() else "cpu"
108
+ inputs = tokenizer(full_prompt, return_tensors="pt").to(device)
109
+
110
+ with torch.no_grad():
111
+ outputs = model.generate(
112
+ **inputs,
113
+ max_new_tokens=256,
114
+ temperature=0.7,
115
+ top_p=0.9,
116
+ do_sample=True
117
+ )
118
+
119
+ # Decode and clean the result
120
+ response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
121
+ clean_response = response_text.split("[/INST]")[-1].strip().strip('"').strip()
122
+
123
+ return {"response": clean_response}
124
+
125
+ @app.get("/")
126
+ def read_root():
127
+ return {"message": "Welcome to my Hugging Face Space!"}
requirements.txt CHANGED
@@ -9,6 +9,6 @@ datasets==2.18.0
9
  fastapi==0.110.0
10
  uvicorn==0.25.0
11
  numpy<2.0
12
-
13
 
14
 
 
9
  fastapi==0.110.0
10
  uvicorn==0.25.0
11
  numpy<2.0
12
+ spacy
13
 
14