Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Retrieve Groq API key from environment variable
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
# Local fallback pipelines (replace with Groq API calls as desired)
|
| 9 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 10 |
+
generator = pipeline("text-generation", model="gpt2", max_length=150)
|
| 11 |
+
summarizer = pipeline("summarization")
|
| 12 |
+
|
| 13 |
+
def email_assistant(email_text):
|
| 14 |
+
# Example: If using Groq API, replace below local classifier code with API call using GROQ_API_KEY
|
| 15 |
+
|
| 16 |
+
candidate_labels = ["Sales", "Support", "Spam", "Finance", "General"]
|
| 17 |
+
classification = classifier(email_text, candidate_labels)
|
| 18 |
+
category = classification['labels'][0]
|
| 19 |
+
confidence = round(classification['scores'][0]*100, 2)
|
| 20 |
+
|
| 21 |
+
priority = "High" if category in ["Sales", "Support"] and confidence > 80 else "Normal"
|
| 22 |
+
|
| 23 |
+
forwarding_emails = {
|
| 24 |
+
"Sales": "sales@company.com",
|
| 25 |
+
"Support": "support@company.com",
|
| 26 |
+
"Finance": "finance@company.com",
|
| 27 |
+
"Spam": None,
|
| 28 |
+
"General": "info@company.com"
|
| 29 |
+
}
|
| 30 |
+
suggested_action = f"Forward to {forwarding_emails[category]}" if forwarding_emails[category] else "No action suggested"
|
| 31 |
+
|
| 32 |
+
# Example: For Groq API generation call, replace below with API call using GROQ_API_KEY
|
| 33 |
+
response_prompt = f"Write a professional reply to this email:\n\n{email_text}\n\nResponse:"
|
| 34 |
+
response = generator(response_prompt, max_length=150, num_return_sequences=1)[0]['generated_text']
|
| 35 |
+
|
| 36 |
+
# Example: For Groq API summarization call, replace below with API call using GROQ_API_KEY
|
| 37 |
+
action_items = summarizer(email_text, max_length=50, min_length=10, do_sample=False)[0]['summary_text']
|
| 38 |
+
|
| 39 |
+
return {
|
| 40 |
+
"Category": category,
|
| 41 |
+
"Confidence": f"{confidence}%",
|
| 42 |
+
"Priority": priority,
|
| 43 |
+
"Suggested Action": suggested_action,
|
| 44 |
+
"Draft Response": response,
|
| 45 |
+
"Action Items": action_items
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("# Email Classification & Response Assistant")
|
| 50 |
+
email_input = gr.TextArea(label="Paste your email here")
|
| 51 |
+
submit = gr.Button("Analyze Email")
|
| 52 |
+
output_category = gr.Textbox(label="Category")
|
| 53 |
+
output_confidence = gr.Textbox(label="Confidence")
|
| 54 |
+
output_priority = gr.Textbox(label="Priority")
|
| 55 |
+
output_action = gr.Textbox(label="Suggested Action")
|
| 56 |
+
output_response = gr.Textbox(label="Draft Response")
|
| 57 |
+
output_items = gr.Textbox(label="Extracted Action Items")
|
| 58 |
+
submit.click(email_assistant, inputs=email_input, outputs=[
|
| 59 |
+
output_category, output_confidence, output_priority,
|
| 60 |
+
output_action, output_response, output_items
|
| 61 |
+
])
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|