Atulkumar001 commited on
Commit
935bcf0
·
verified ·
1 Parent(s): 61de2cf

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +212 -0
app.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Untitled0.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1jmDagWzG5dZJK1QAfpBnu3n1NXS_QIOz
8
+ """
9
+
10
+ !pip install pandas datasets
11
+
12
+ import pandas as pd
13
+ from datasets import load_dataset
14
+
15
+ # 1. Download the raw dataset from Hugging Face
16
+ raw_data = load_dataset("bitext/Bitext-customer-support-llm-chatbot-training-dataset")
17
+
18
+ # 2. Convert it into a Pandas DataFrame (think of this as a code-based Excel sheet)
19
+ df = pd.DataFrame(raw_data['train'])
20
+
21
+ # 3. Data Cleaning: Drop rows with missing data and remove duplicate queries
22
+ df.dropna(subset=['instruction', 'response'], inplace=True)
23
+ df.drop_duplicates(subset=['instruction'], inplace=True)
24
+
25
+ # 4. Feature Selection: Keep only the columns we actually need for the AI
26
+ df = df[['category', 'instruction', 'response']]
27
+
28
+ # 5. Rename columns so they align with our business logic
29
+ df.columns = ['ticket_category', 'customer_query', 'historical_resolution']
30
+
31
+ # Display the first 5 rows to verify the cleaning worked
32
+ print(df.head())
33
+
34
+ !pip install sentence-transformers
35
+
36
+ from sentence_transformers import SentenceTransformer
37
+ import numpy as np
38
+
39
+ # 1. Load the Multilingual Embedding Model
40
+ # This specific model understands over 50 languages and aligns them in the same semantic space
41
+ print("Downloading the multilingual embedding model (this may take a minute)...")
42
+ model = SentenceTransformer('paraphrase-multilingual-MiniLM-L12-v2')
43
+
44
+ # 2. Extract a sample of our cleaned resolutions
45
+ # We use 100 rows to quickly test the pipeline
46
+ sample_resolutions = df['historical_resolution'].head(100).tolist()
47
+
48
+ # 3. Convert the text into vectors
49
+ print("Generating embeddings for the resolutions...")
50
+ resolution_embeddings = model.encode(sample_resolutions)
51
+
52
+ # 4. Verify the output
53
+ print(f"\nSuccessfully created {len(resolution_embeddings)} vector embeddings.")
54
+ print(f"Each vector is an array of {len(resolution_embeddings[0])} numbers.")
55
+ print("Here are the first 5 numbers of the first vector:")
56
+ print(resolution_embeddings[0][:5])
57
+
58
+ from sentence_transformers import util
59
+
60
+ # 1. Simulate an incoming customer ticket in German
61
+ # Meaning: "How do I create a new account?"
62
+ customer_query = "Wie erstelle ich ein neues Konto?"
63
+ print(f"Incoming Customer Query (German): '{customer_query}'\n")
64
+
65
+ # 2. Embed the query using the exact same model
66
+ query_embedding = model.encode(customer_query)
67
+
68
+ # 3. Perform the Semantic Search
69
+ # This calculates the cosine similarity between the query vector and all 100 stored resolution vectors
70
+ # top_k=1 tells the system to only return the single best match
71
+ hits = util.semantic_search(query_embedding, resolution_embeddings, top_k=1)
72
+
73
+ # 4. Extract the results
74
+ best_match_index = hits[0][0]['corpus_id']
75
+ similarity_score = hits[0][0]['score']
76
+
77
+ # 5. Display the matching document from our original Pandas DataFrame
78
+ print(f"Similarity Score: {similarity_score:.2f} (1.0 is a perfect semantic match)\n")
79
+ print("--- Retrieved Internal Resolution Document (English) ---")
80
+ print(df['historical_resolution'].iloc[best_match_index])
81
+
82
+ !pip install huggingface_hub
83
+
84
+ from huggingface_hub import InferenceClient
85
+
86
+ # 1. Initialize the client with your token
87
+ # We are using a highly capable open-source instruction model
88
+ client = InferenceClient(
89
+ model="HuggingFaceH4/zephyr-7b-beta",
90
+ token="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"
91
+ )
92
+
93
+ # 2. Extract the retrieved English document from the previous step
94
+ retrieved_context = df['historical_resolution'].iloc[best_match_index]
95
+
96
+ # 3. Construct the Prompt
97
+ # This is the "Prompt Engineering" layer where we instruct the LLM on how to behave
98
+ prompt = f"""
99
+ You are a professional customer support agent for a tech company.
100
+ A customer has asked the following query: "{customer_query}"
101
+
102
+ Using ONLY the information provided in the internal resolution document below, write a polite and helpful email response directly to the customer.
103
+ The response MUST be in the same language as the customer's query. Do not invent any new steps.
104
+
105
+ Internal Resolution Document:
106
+ {retrieved_context}
107
+
108
+ Final Response:
109
+ """
110
+
111
+ # 4. Generate the Response
112
+ print("Generating final response via LLM...\n")
113
+ try:
114
+ response = client.text_generation(
115
+ prompt,
116
+ max_new_tokens=250, # Limits the length of the response
117
+ temperature=0.3, # Keeps the response focused and factual rather than "creative"
118
+ )
119
+ print("--- Final Customer Email (German) ---")
120
+ print(response.strip())
121
+ except Exception as e:
122
+ print(f"Error during generation: {e}")
123
+
124
+ from huggingface_hub import InferenceClient
125
+
126
+ # 1. Initialize the client using the highly stable Llama-3 model
127
+ client = InferenceClient(
128
+ model="meta-llama/Meta-Llama-3-8B-Instruct",
129
+ token="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"
130
+ )
131
+
132
+ # 2. Extract the retrieved English document from our database
133
+ retrieved_context = df['historical_resolution'].iloc[best_match_index]
134
+
135
+ # 3. Construct the Chat Messages
136
+ # Instead of a raw string, modern APIs use a structured list of messages
137
+ messages = [
138
+ {
139
+ "role": "system",
140
+ "content": "You are a professional customer support agent. Use ONLY the provided Internal Document to answer the user. Respond in the exact same language as the user's query."
141
+ },
142
+ {
143
+ "role": "user",
144
+ "content": f"Customer Query: {customer_query}\n\nInternal Document: {retrieved_context}"
145
+ }
146
+ ]
147
+
148
+ # 4. Generate the Response using the modern Chat Completion API
149
+ print("Generating final response via LLM...\n")
150
+ try:
151
+ response = client.chat.completions.create(
152
+ messages=messages,
153
+ max_tokens=250,
154
+ temperature=0.3,
155
+ )
156
+
157
+ print("--- Final Customer Email (German) ---")
158
+ # Extracting the text from the structured API response
159
+ print(response.choices[0].message.content.strip())
160
+
161
+ except Exception as e:
162
+ print(f"Error during generation: {e}")
163
+
164
+ # 1. Install Gradio (Run this in a cell if in Colab: !pip install gradio)
165
+ import gradio as gr
166
+
167
+ # 2. Define the core function that ties everything together
168
+ def resolve_ticket(customer_query):
169
+ # Embed the query
170
+ query_embedding = model.encode(customer_query)
171
+
172
+ # Retrieve the best match
173
+ hits = util.semantic_search(query_embedding, resolution_embeddings, top_k=1)
174
+ best_match_index = hits[0][0]['corpus_id']
175
+ retrieved_context = df['historical_resolution'].iloc[best_match_index]
176
+
177
+ # Prompt the LLM
178
+ messages = [
179
+ {"role": "system", "content": "You are a professional customer support agent. Use ONLY the provided Internal Document to answer the user. Respond in the exact same language as the user's query."},
180
+ {"role": "user", "content": f"Customer Query: {customer_query}\n\nInternal Document: {retrieved_context}"}
181
+ ]
182
+
183
+ # Generate the response
184
+ try:
185
+ response = client.chat.completions.create(
186
+ messages=messages,
187
+ max_tokens=250,
188
+ temperature=0.3,
189
+ )
190
+ return response.choices[0].message.content.strip(), retrieved_context
191
+ except Exception as e:
192
+ return f"Error: {e}", retrieved_context
193
+
194
+ # 3. Build the Web Interface
195
+ with gr.Blocks() as demo:
196
+ gr.Markdown("# 🌍 Multilingual AI Support Triage")
197
+ gr.Markdown("Type a support query in any language. The system will retrieve the correct English internal document and draft a reply in your language.")
198
+
199
+ with gr.Row():
200
+ with gr.Column():
201
+ input_query = gr.Textbox(lines=4, label="Incoming Customer Ticket (Try German, French, etc.)")
202
+ submit_btn = gr.Button("Resolve Ticket")
203
+
204
+ with gr.Column():
205
+ output_reply = gr.Textbox(lines=6, label="Generated Email Response")
206
+ retrieved_doc = gr.Textbox(lines=4, label="Retrieved Internal Document (English)")
207
+
208
+ # Connect the button to the function
209
+ submit_btn.click(fn=resolve_ticket, inputs=input_query, outputs=[output_reply, retrieved_doc])
210
+
211
+ # 4. Launch the App
212
+ demo.launch()