File size: 3,187 Bytes
a74cfea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c91749d
 
a74cfea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c91749d
a74cfea
 
 
 
 
 
c91749d
a74cfea
 
 
 
c91749d
 
 
 
 
 
 
 
a74cfea
c91749d
 
 
a74cfea
c91749d
 
 
 
a74cfea
 
 
 
c91749d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import os
import torch
import gradio as gr
from langchain import HuggingFaceHub
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from transformers import pipeline

# Initialize sentiment analyzer
sentiment_analyzer = pipeline(
    "sentiment-analysis",
    model="finiteautomata/bertweet-base-sentiment-analysis",
    device=0 if torch.cuda.is_available() else -1
)

# Initialize LLM
llm = HuggingFaceHub(
    repo_id="deepseek-ai/deepseek-coder-33b-instruct",
    model_kwargs={"temperature": 0.7},
    huggingfacehub_api_token=os.environ.get("HUGGINGFACEHUB_API_TOKEN")
)

email_template = PromptTemplate(
    input_variables=["previous_interaction", "situation_type", "tone", "urgency"],
    template="""Based on these details, generate a professional follow-up email:

Previous Interaction: {previous_interaction}
Situation Type: {situation_type}
Tone: {tone}
Urgency Level: {urgency}

Generate a personalized email that:
1. Maintains {tone} tone
2. Addresses the specific situation
3. Provides clear next steps
4. Is appropriate for {urgency} urgency level
"""
)

email_chain = LLMChain(llm=llm, prompt=email_template)

def analyze_sentiment(text):
    try:
        result = sentiment_analyzer(text)[0]
        sentiment_to_tone = {
            'POS': 'Friendly',
            'NEU': 'Professional',
            'NEG': 'Apologetic'
        }
        return sentiment_to_tone.get(result['label'], 'Professional')
    except Exception as e:
        return 'Professional'

def generate_followup_email(previous_interaction, situation_type, tone, urgency):
    try:
        if not tone:
            tone = analyze_sentiment(previous_interaction)
        return email_chain.run({
            "previous_interaction": previous_interaction,
            "situation_type": situation_type,
            "tone": tone,
            "urgency": urgency
        })
    except Exception as e:
        return f"Error generating email: {str(e)}"

demo = gr.Interface(
    fn=generate_followup_email,
    inputs=[
        gr.Textbox(label="Previous Interaction", lines=5, 
                  placeholder="Describe the previous interaction with the customer..."),
        gr.Dropdown(label="Situation Type",
                   choices=["Complaint Resolution", "Service Issue", 
                           "Payment Dispute", "Product Query", "General Follow-up"]),
        gr.Dropdown(label="Tone (Optional - will be automatically detected if not specified)",
                   choices=["", "Professional", "Apologetic", "Friendly", "Formal", "Empathetic"]),
        gr.Dropdown(label="Urgency", choices=["High", "Medium", "Low"])
    ],
    outputs=gr.Textbox(label="Generated Email"),
    title="Smart Sales Email Generator",
    description="Generate personalized follow-up emails based on previous interactions",
    examples=[
        ["Customer complained about slow website loading times and threatened to cancel subscription", 
         "Complaint Resolution", "Apologetic", "High"],
        ["Client requested information about premium features and pricing", 
         "Product Query", "Professional", "Medium"]
    ]
)

if __name__ == "__main__":
    demo.launch()