Spaces:
Build error
Build error
Upload folder using huggingface_hub
Browse files- .gitignore +11 -0
- .gradio/certificate.pem +31 -0
- .python-version +1 -0
- README.md +138 -12
- agents.py +72 -0
- app.py +125 -0
- data.csv +5 -0
- form_prompt.txt +2 -0
- main.py +30 -0
- pyproject.toml +15 -0
- requirments.txt +0 -0
- response_prompt.txt +1 -0
- settings.py +11 -0
- uv.lock +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python-generated files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[oc]
|
| 4 |
+
build/
|
| 5 |
+
dist/
|
| 6 |
+
wheels/
|
| 7 |
+
*.egg-info
|
| 8 |
+
|
| 9 |
+
# Virtual environments
|
| 10 |
+
.venv
|
| 11 |
+
.env
|
.gradio/certificate.pem
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-----BEGIN CERTIFICATE-----
|
| 2 |
+
MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
|
| 3 |
+
TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
|
| 4 |
+
cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
|
| 5 |
+
WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Customer_Support_Agent
|
| 3 |
+
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 5.23.3
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
# 💬 Customer Support Assistant
|
| 9 |
+
|
| 10 |
+
An AI-powered voice + text chatbot built with [`pydantic_ai`](https://github.com/roboflow/pydantic-ai), powered by Llama 3.3 70B via Cerebras/Groq. It processes customer issues, detects emotional tone, and records support requests into a structured data table. Deployable on Gradio Spaces and usable locally with both text and audio input.
|
| 11 |
+
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
## ⚡ Features
|
| 15 |
+
|
| 16 |
+
- 🔥 **LLM-powered form extraction** using `Agent` abstraction from `pydantic_ai`
|
| 17 |
+
- 🎙️ **Voice chat** with real-time streaming via [`fastrtc`](https://github.com/Rikhil-Rai/fastrtc)
|
| 18 |
+
- 🧠 **Memory-aware responses** using `message_history`
|
| 19 |
+
- 📊 **Live DataFrame updates** for structured customer requests
|
| 20 |
+
- 💾 **Persistent CSV logging**
|
| 21 |
+
- 🛠️ One-click Gradio UI with tabs for Chat + Customer Data
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## 🚀 Getting Started
|
| 26 |
+
|
| 27 |
+
### 1. Clone the Repo
|
| 28 |
+
|
| 29 |
+
```bash
|
| 30 |
+
git clone https://github.com/your-username/customer-support-assistant
|
| 31 |
+
cd customer-support-assistant
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
### 2. Install Dependencies
|
| 35 |
+
|
| 36 |
+
> ⚠️ Make sure you use `uv` to ensure proper dependency resolution (especially for `pydantic_ai`).
|
| 37 |
+
|
| 38 |
+
```bash
|
| 39 |
+
uv pip install -r requirements.txt
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
> Or use `uv` directly:
|
| 43 |
+
|
| 44 |
+
```bash
|
| 45 |
+
uv venv
|
| 46 |
+
source .venv/bin/activate
|
| 47 |
+
uv pip install -r requirements.txt
|
| 48 |
+
```
|
| 49 |
+
|
| 50 |
+
---
|
| 51 |
+
|
| 52 |
+
### 3. Setup `.env` File
|
| 53 |
+
|
| 54 |
+
Create a `.env` file in the root directory.
|
| 55 |
+
|
| 56 |
+
Refer to `settings.py` for required fields. At a minimum, you will need:
|
| 57 |
+
|
| 58 |
+
```env
|
| 59 |
+
CEREBRAS_API_KEY=your_api_key
|
| 60 |
+
CEREBRAS_BASE_URL=https://api.groq.com/openai/v1
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
You can optionally set environment variables for your own STT/TTS models as required by `fastrtc`.
|
| 64 |
+
|
| 65 |
+
---
|
| 66 |
+
|
| 67 |
+
### 4. Run the App Locally
|
| 68 |
+
|
| 69 |
+
```bash
|
| 70 |
+
python app.py
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
## 🌐 Deploy on Gradio Spaces
|
| 76 |
+
|
| 77 |
+
1. Add your `CEREBRAS_API_KEY` and `CEREBRAS_BASE_URL` as secrets in the Gradio Space.
|
| 78 |
+
2. Make sure `fastrtc` audio support is configured in the hardware tab.
|
| 79 |
+
3. Gradio Spaces will auto-launch the app via `app.py`.
|
| 80 |
+
|
| 81 |
+
---
|
| 82 |
+
|
| 83 |
+
## 🧪 Debug Tips
|
| 84 |
+
|
| 85 |
+
To simulate extraction alone:
|
| 86 |
+
|
| 87 |
+
```bash
|
| 88 |
+
python agents.py
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
Then type messages in the CLI to test how well the form is filled.
|
| 92 |
+
|
| 93 |
+
---
|
| 94 |
+
|
| 95 |
+
## 📁 File Structure
|
| 96 |
+
|
| 97 |
+
```plaintext
|
| 98 |
+
├── app.py # Main Gradio UI
|
| 99 |
+
├── agents.py # Agent config + LLM interaction logic
|
| 100 |
+
├── settings.py # Handles env/config
|
| 101 |
+
├── form_prompt.txt # System prompt for form extraction
|
| 102 |
+
├── response_prompt.txt # System prompt for response generation
|
| 103 |
+
├── data.csv # Auto-generated CSV storage
|
| 104 |
+
├── requirements.txt
|
| 105 |
+
├── README.md
|
| 106 |
+
```
|
| 107 |
+
|
| 108 |
+
---
|
| 109 |
+
|
| 110 |
+
## 🧠 What the AI Extracts
|
| 111 |
+
|
| 112 |
+
The form agent will pull out:
|
| 113 |
+
|
| 114 |
+
- `customername`: Extracted from input or marked as `"unknown"`
|
| 115 |
+
- `requesttype`: e.g., `"billing"`, `"technical support"`
|
| 116 |
+
- `issue`: 50-line description of the issue
|
| 117 |
+
- `emotion`: `"angry"`, `"happy"`, etc.
|
| 118 |
+
|
| 119 |
+
---
|
| 120 |
+
|
| 121 |
+
## 🛑 Known Limitations
|
| 122 |
+
|
| 123 |
+
- Longform audio may require silence-based segmentation tuning.
|
| 124 |
+
- This project assumes inputs are customer support related. General queries may misfire.
|
| 125 |
+
- Error handling is basic — add guards if scaling for production use.
|
| 126 |
+
|
| 127 |
+
---
|
| 128 |
+
|
| 129 |
+
## 🙏 Credits
|
| 130 |
+
|
| 131 |
+
- Built using [pydantic_ai](https://github.com/roboflow/pydantic-ai)
|
| 132 |
+
- Voice support via [fastrtc](https://github.com/Rikhil-Rai/fastrtc)
|
| 133 |
+
- LLM backend: Llama 3.3 70B via [Groq](https://groq.com/)
|
| 134 |
+
- UI powered by [Gradio](https://gradio.app/)
|
| 135 |
+
|
| 136 |
+
---
|
| 137 |
+
|
| 138 |
+
## ❤️ Made with care by Rikhil
|
agents.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic_ai import Agent, RunContext
|
| 2 |
+
import openai
|
| 3 |
+
from pydantic_ai.models.openai import OpenAIModelSettings, OpenAIModel
|
| 4 |
+
from pydantic_ai.providers.openai import OpenAIProvider
|
| 5 |
+
from pydantic import BaseModel, Field
|
| 6 |
+
from settings import Settings
|
| 7 |
+
import asyncio
|
| 8 |
+
from dataclasses import dataclass
|
| 9 |
+
|
| 10 |
+
settings = Settings()
|
| 11 |
+
|
| 12 |
+
groq_settings = OpenAIModelSettings(
|
| 13 |
+
temperature=0.7,
|
| 14 |
+
top_p=0.95,
|
| 15 |
+
frequency_penalty=0,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
model_name = "llama-3.3-70b"
|
| 19 |
+
|
| 20 |
+
client = openai.AsyncOpenAI(api_key=settings.cerebras_api_key, base_url=settings.cerebras_base_url)
|
| 21 |
+
|
| 22 |
+
model = OpenAIModel(
|
| 23 |
+
model_name=model_name,
|
| 24 |
+
provider=OpenAIProvider(openai_client=client),
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
@dataclass
|
| 28 |
+
class Deps:
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
class Form(BaseModel):
|
| 32 |
+
customername: str = Field(description="The name of the customer making the request if given, else 'unknown'")
|
| 33 |
+
requesttype: str = Field(description="The type of request being made. example: 'technical support', 'billing', etc.")
|
| 34 |
+
issue: str = Field(description="Detailed description of 50 lines of the issue being reported by the customer")
|
| 35 |
+
emotion: str = Field(description="The emotion of the customer to be given in one word. example: 'angry', 'happy', 'sad', etc.")
|
| 36 |
+
|
| 37 |
+
with open("form_prompt.txt", "r") as file:
|
| 38 |
+
form_prompt = file.read()
|
| 39 |
+
|
| 40 |
+
with open("response_prompt.txt", "r") as file:
|
| 41 |
+
response_prompt = file.read()
|
| 42 |
+
|
| 43 |
+
form_agent = Agent(
|
| 44 |
+
model=model,
|
| 45 |
+
model_settings=groq_settings,
|
| 46 |
+
system_prompt=form_prompt,
|
| 47 |
+
retries=3,
|
| 48 |
+
result_type=Form,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
response_agent = Agent(
|
| 52 |
+
model=model,
|
| 53 |
+
model_settings=groq_settings,
|
| 54 |
+
system_prompt=response_prompt,
|
| 55 |
+
retries=3,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Code below is only for debugging please ignore
|
| 59 |
+
|
| 60 |
+
async def chat():
|
| 61 |
+
while True:
|
| 62 |
+
user_message = input("You: ")
|
| 63 |
+
|
| 64 |
+
if user_message == "exit":
|
| 65 |
+
break
|
| 66 |
+
|
| 67 |
+
result = await form_agent.run(user_prompt=user_message)
|
| 68 |
+
response = result.data if result else "Sorry, I failed to process that."
|
| 69 |
+
|
| 70 |
+
print("Bot:", response)
|
| 71 |
+
|
| 72 |
+
# asyncio.run(chat())
|
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import atexit
|
| 5 |
+
from fastrtc import WebRTC, ReplyOnPause, get_stt_model, get_tts_model
|
| 6 |
+
from settings import Settings
|
| 7 |
+
from pydantic_ai.messages import (
|
| 8 |
+
ModelMessage,
|
| 9 |
+
ModelRequest,
|
| 10 |
+
ModelResponse,
|
| 11 |
+
UserPromptPart,
|
| 12 |
+
TextPart
|
| 13 |
+
)
|
| 14 |
+
from agents import form_agent, response_agent
|
| 15 |
+
|
| 16 |
+
# Config and Globals
|
| 17 |
+
settings = Settings()
|
| 18 |
+
stt_model = get_stt_model()
|
| 19 |
+
tts_model = get_tts_model()
|
| 20 |
+
messages: list[ModelMessage] = []
|
| 21 |
+
|
| 22 |
+
DATA_PATH = "data.csv"
|
| 23 |
+
df = pd.read_csv(DATA_PATH) if os.path.exists(DATA_PATH) else pd.DataFrame(columns=["customer_name", "request_type", "issue", "emotion"])
|
| 24 |
+
|
| 25 |
+
def save_data_on_exit():
|
| 26 |
+
df.to_csv(DATA_PATH, index=False)
|
| 27 |
+
|
| 28 |
+
atexit.register(save_data_on_exit)
|
| 29 |
+
|
| 30 |
+
def df_update():
|
| 31 |
+
global df
|
| 32 |
+
try:
|
| 33 |
+
form_response = form_agent.run_sync(user_prompt="Do your thing", message_history=messages)
|
| 34 |
+
new_row = {
|
| 35 |
+
"customer_name": form_response.data.customername,
|
| 36 |
+
"request_type": form_response.data.requesttype,
|
| 37 |
+
"issue": form_response.data.issue,
|
| 38 |
+
"emotion": form_response.data.emotion
|
| 39 |
+
}
|
| 40 |
+
df = pd.concat([df, pd.DataFrame([new_row])], ignore_index=True)
|
| 41 |
+
df.to_csv(DATA_PATH, index=False)
|
| 42 |
+
return "✅ DataFrame updated successfully!"
|
| 43 |
+
except Exception as e:
|
| 44 |
+
return f"❌ Update failed: {str(e)}"
|
| 45 |
+
|
| 46 |
+
def update_table():
|
| 47 |
+
global df
|
| 48 |
+
if os.path.exists(DATA_PATH):
|
| 49 |
+
df = pd.read_csv(DATA_PATH)
|
| 50 |
+
else:
|
| 51 |
+
df = pd.DataFrame(columns=["customer_name", "request_type", "issue", "emotion"])
|
| 52 |
+
return df
|
| 53 |
+
|
| 54 |
+
def reset_memory():
|
| 55 |
+
global messages
|
| 56 |
+
messages = []
|
| 57 |
+
return "🧠 Memory reset successfully."
|
| 58 |
+
|
| 59 |
+
async def handle_audio(audio):
|
| 60 |
+
prompt = stt_model.stt(audio)
|
| 61 |
+
response_text = await response_agent.run(user_prompt=prompt, message_history=messages)
|
| 62 |
+
messages.append(ModelRequest(parts=[UserPromptPart(content=prompt)]))
|
| 63 |
+
messages.append(ModelResponse(parts=[TextPart(content=response_text.data)]))
|
| 64 |
+
for chunk in tts_model.stream_tts(response_text.data):
|
| 65 |
+
yield chunk
|
| 66 |
+
|
| 67 |
+
async def handle_text_chat(user_text, history):
|
| 68 |
+
response = await response_agent.run(user_prompt=user_text, message_history=messages)
|
| 69 |
+
messages.append(ModelRequest(parts=[UserPromptPart(content=user_text)]))
|
| 70 |
+
messages.append(ModelResponse(parts=[TextPart(content=response.data)]))
|
| 71 |
+
history = history + [[user_text, response.data]]
|
| 72 |
+
return "", history
|
| 73 |
+
|
| 74 |
+
# Gradio UI
|
| 75 |
+
with gr.Blocks(css="""
|
| 76 |
+
.toolbox { display: flex; gap: 0.5rem; margin-top: 0.5rem; }
|
| 77 |
+
.footer-note { text-align: center; font-size: 0.85rem; color: #666; margin-top: 1rem; }
|
| 78 |
+
""") as demo:
|
| 79 |
+
gr.Markdown("<h2 style='text-align: center;'>💬 Customer Support Assistant</h2>")
|
| 80 |
+
|
| 81 |
+
debug_box = gr.Textbox(visible=False)
|
| 82 |
+
|
| 83 |
+
with gr.Tabs():
|
| 84 |
+
with gr.Tab("Chat"):
|
| 85 |
+
with gr.Row():
|
| 86 |
+
with gr.Column(scale=3):
|
| 87 |
+
chatbot = gr.Chatbot(label="Chat Interface")
|
| 88 |
+
user_input = gr.Textbox(placeholder="Type your message...", show_label=False)
|
| 89 |
+
user_input.submit(fn=handle_text_chat, inputs=[user_input, chatbot], outputs=[user_input, chatbot])
|
| 90 |
+
|
| 91 |
+
with gr.Column(scale=1):
|
| 92 |
+
mic_button = WebRTC(mode="send-receive", modality="audio")
|
| 93 |
+
mic_button.stream(fn=ReplyOnPause(handle_audio), inputs=[mic_button], outputs=[mic_button], time_limit=60)
|
| 94 |
+
|
| 95 |
+
with gr.Tab("Customer Data"):
|
| 96 |
+
gr.Markdown("### Customer Information Table")
|
| 97 |
+
data_frame = gr.Dataframe(
|
| 98 |
+
headers=["customer_name", "request_type", "issue", "emotion"],
|
| 99 |
+
interactive=False,
|
| 100 |
+
wrap=True
|
| 101 |
+
)
|
| 102 |
+
with gr.Row(elem_classes="toolbox"):
|
| 103 |
+
update_button = gr.Button("📤 Update DataFrame")
|
| 104 |
+
refresh_button = gr.Button("🔄 Refresh Table")
|
| 105 |
+
reset_button = gr.Button("🪹 Reset Memory")
|
| 106 |
+
|
| 107 |
+
update_button.click(fn=df_update, outputs=[debug_box])
|
| 108 |
+
refresh_button.click(fn=update_table, outputs=[data_frame])
|
| 109 |
+
reset_button.click(fn=reset_memory, outputs=[debug_box])
|
| 110 |
+
|
| 111 |
+
# Toast feedback
|
| 112 |
+
def show_toast(msg: str):
|
| 113 |
+
if msg:
|
| 114 |
+
gr.Info(msg)
|
| 115 |
+
|
| 116 |
+
debug_box.change(fn=show_toast, inputs=[debug_box])
|
| 117 |
+
|
| 118 |
+
# Footer
|
| 119 |
+
gr.Markdown("<div class='footer-note'>🚀 Made with ❤️ by Rikhil</div>")
|
| 120 |
+
|
| 121 |
+
demo.load(fn=update_table, outputs=[data_frame])
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
demo.launch()
|
data.csv
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
customer_name,request_type,issue,emotion
|
| 2 |
+
Rikhil,technical support,"The customer, Rikhil, is experiencing issues with the quality of their TV and the installation team has not been responsive.",devastated
|
| 3 |
+
unknown,unknown,The customer did not provide any information about their issue.,unknown
|
| 4 |
+
Sanjana,Installation Support,The customer needs help with the installation of her fridge.,helpless
|
| 5 |
+
Nishant,Technical Support,The suction power of the vacuum cleaner has decreased. The customer has tried troubleshooting but the issue persists. A technician is required to check and repair the device.,Frustrated
|
form_prompt.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are an AI chatbot tasked with providing extra support to a human operator who is receiving a customer support request.
|
| 2 |
+
You will be provided with a transcript of the conversation.
|
main.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List
|
| 2 |
+
from fastrtc import (ReplyOnPause, Stream, get_stt_model, get_tts_model)
|
| 3 |
+
from agent import agent
|
| 4 |
+
from pydantic_ai.messages import ModelMessage, ModelRequest, ModelResponse, UserPromptPart, TextPart
|
| 5 |
+
from settings import Settings
|
| 6 |
+
|
| 7 |
+
settings = Settings()
|
| 8 |
+
hf_token = settings.hf_token
|
| 9 |
+
stt_model = get_stt_model()
|
| 10 |
+
tts_model = get_tts_model()
|
| 11 |
+
|
| 12 |
+
messages: List[ModelMessage] = []
|
| 13 |
+
|
| 14 |
+
def echo(audio):
|
| 15 |
+
prompt = stt_model.stt(audio)
|
| 16 |
+
|
| 17 |
+
response = agent.run_sync(user_prompt=prompt, message_history=messages)
|
| 18 |
+
|
| 19 |
+
messages.append(ModelRequest(parts=[UserPromptPart(content=prompt)]))
|
| 20 |
+
messages.append(ModelResponse(parts=[TextPart(content=response.data)]))
|
| 21 |
+
|
| 22 |
+
for audio_chunk in tts_model.stream_tts_sync(response.data):
|
| 23 |
+
yield audio_chunk
|
| 24 |
+
|
| 25 |
+
stream = Stream(
|
| 26 |
+
handler=ReplyOnPause(echo),
|
| 27 |
+
modality="audio",
|
| 28 |
+
mode="send-receive")
|
| 29 |
+
|
| 30 |
+
stream.ui.launch()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "CustomerSupportAgent"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.13"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"fastrtc[stt,tts,vad]>=0.0.19",
|
| 9 |
+
"gradio>=5.23.3",
|
| 10 |
+
"openai>=1.70.0",
|
| 11 |
+
"pandas>=2.2.3",
|
| 12 |
+
"pydantic>=2.11.2",
|
| 13 |
+
"pydantic-ai>=0.0.52",
|
| 14 |
+
"pydantic-settings>=2.8.1",
|
| 15 |
+
]
|
requirments.txt
ADDED
|
Binary file (5.77 kB). View file
|
|
|
response_prompt.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
You are a customer support bot and are to be very courteous to the customers who come with complaints, never claim it is out of your scope always guide them
|
settings.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pydantic_settings import BaseSettings
|
| 2 |
+
from pydantic import Field
|
| 3 |
+
|
| 4 |
+
class Settings(BaseSettings):
|
| 5 |
+
|
| 6 |
+
cerebras_api_key : str = Field(..., validate_alias="CEREBRAS_API_KEY")
|
| 7 |
+
cerebras_base_url : str = Field(..., validate_alias="CEREBRAS_BASE_URL")
|
| 8 |
+
|
| 9 |
+
class Config:
|
| 10 |
+
env_file = ".env"
|
| 11 |
+
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|