Spaces:
Sleeping
Sleeping
Commit
·
204d33b
1
Parent(s):
dc6ab46
Refactor app.py to implement streaming response generation for email creation. Removed tokenizer and model initialization in favor of using the InferenceClient for improved performance and reduced complexity. Updated conversation_predict function to yield streaming output, enhancing user experience with real-time email generation feedback. Added a new update_ui function to manage email display in the Streamlit interface, ensuring a smoother interaction flow.
Browse files
app.py
CHANGED
|
@@ -78,15 +78,6 @@ def extract_cv_text(file):
|
|
| 78 |
# Replace 'your_huggingface_token' with your actual Hugging Face access token
|
| 79 |
access_token = os.getenv('API_KEY')
|
| 80 |
|
| 81 |
-
# Initialize the tokenizer and model with the Hugging Face access token
|
| 82 |
-
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", use_auth_token=access_token)
|
| 83 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 84 |
-
"google/gemma-2b-it",
|
| 85 |
-
torch_dtype=torch.bfloat16,
|
| 86 |
-
use_auth_token=access_token
|
| 87 |
-
)
|
| 88 |
-
model.eval() # Set the model to evaluation mode
|
| 89 |
-
|
| 90 |
# Initialize the inference client (if needed for other API-based tasks)
|
| 91 |
client = InferenceClient(token=access_token)
|
| 92 |
|
|
@@ -118,23 +109,22 @@ Keep the tone professional, confident, and enthusiastic. Be concise but impactfu
|
|
| 118 |
Email:"""
|
| 119 |
|
| 120 |
def conversation_predict(input_text: str, cv_sections: Dict[str, str]):
|
| 121 |
-
"""Generate a response using the model with
|
| 122 |
prompt = create_email_prompt(input_text, cv_sections)
|
| 123 |
|
| 124 |
-
#
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
input_ids,
|
| 130 |
-
max_new_tokens=2048,
|
| 131 |
temperature=0.7,
|
| 132 |
top_p=0.95,
|
| 133 |
-
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
| 138 |
|
| 139 |
def respond(
|
| 140 |
message: str,
|
|
@@ -194,18 +184,34 @@ with tab1:
|
|
| 194 |
if isinstance(cv_sections, dict):
|
| 195 |
st.success("CV uploaded and parsed successfully!")
|
| 196 |
else:
|
| 197 |
-
st.error(cv_sections)
|
| 198 |
|
| 199 |
# Job description input
|
| 200 |
st.markdown("### Job Description")
|
| 201 |
message = st.text_area("Paste the job description here:", height=200)
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
# Generate button
|
| 204 |
if st.button("Generate Email"):
|
| 205 |
if message and cv_file and isinstance(cv_sections, dict):
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
else:
|
| 210 |
st.warning("Please upload a CV and enter a job description.")
|
| 211 |
|
|
|
|
| 78 |
# Replace 'your_huggingface_token' with your actual Hugging Face access token
|
| 79 |
access_token = os.getenv('API_KEY')
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
# Initialize the inference client (if needed for other API-based tasks)
|
| 82 |
client = InferenceClient(token=access_token)
|
| 83 |
|
|
|
|
| 109 |
Email:"""
|
| 110 |
|
| 111 |
def conversation_predict(input_text: str, cv_sections: Dict[str, str]):
|
| 112 |
+
"""Generate a response using the model with streaming output."""
|
| 113 |
prompt = create_email_prompt(input_text, cv_sections)
|
| 114 |
|
| 115 |
+
# Use the streaming API
|
| 116 |
+
for response in client.text_generation(
|
| 117 |
+
model="google/gemma-2b-it",
|
| 118 |
+
prompt=prompt,
|
| 119 |
+
max_new_tokens=512,
|
|
|
|
|
|
|
| 120 |
temperature=0.7,
|
| 121 |
top_p=0.95,
|
| 122 |
+
stream=True
|
| 123 |
+
):
|
| 124 |
+
if hasattr(response, 'token'): # Handle different response formats
|
| 125 |
+
yield response.token.text
|
| 126 |
+
else:
|
| 127 |
+
yield response.generated_text
|
| 128 |
|
| 129 |
def respond(
|
| 130 |
message: str,
|
|
|
|
| 184 |
if isinstance(cv_sections, dict):
|
| 185 |
st.success("CV uploaded and parsed successfully!")
|
| 186 |
else:
|
| 187 |
+
st.error(cv_sections)
|
| 188 |
|
| 189 |
# Job description input
|
| 190 |
st.markdown("### Job Description")
|
| 191 |
message = st.text_area("Paste the job description here:", height=200)
|
| 192 |
|
| 193 |
+
# Call the updated UI function
|
| 194 |
+
update_ui()
|
| 195 |
+
|
| 196 |
+
def update_ui():
|
| 197 |
+
# Create placeholder for the generated email
|
| 198 |
+
email_placeholder = st.empty()
|
| 199 |
+
|
| 200 |
# Generate button
|
| 201 |
if st.button("Generate Email"):
|
| 202 |
if message and cv_file and isinstance(cv_sections, dict):
|
| 203 |
+
email_text = ""
|
| 204 |
+
# Stream the response
|
| 205 |
+
for chunk in conversation_predict(message, cv_sections):
|
| 206 |
+
if chunk:
|
| 207 |
+
email_text += chunk
|
| 208 |
+
# Update the text area with each chunk
|
| 209 |
+
email_placeholder.text_area(
|
| 210 |
+
"Generated Email",
|
| 211 |
+
value=email_text,
|
| 212 |
+
height=400,
|
| 213 |
+
key="email_output"
|
| 214 |
+
)
|
| 215 |
else:
|
| 216 |
st.warning("Please upload a CV and enter a job description.")
|
| 217 |
|