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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import httpx
|
| 6 |
+
import time
|
| 7 |
+
from typing import List, Dict
|
| 8 |
+
|
| 9 |
+
class GroqHRGenerator:
|
| 10 |
+
def __init__(self, api_key: str):
|
| 11 |
+
self.api_key = api_key
|
| 12 |
+
self.base_url = "https://api.groq.com/openai/v1/chat/completions"
|
| 13 |
+
self.headers = {
|
| 14 |
+
"Authorization": f"Bearer {api_key}",
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def _call_groq_api(self, prompt: str) -> str:
|
| 19 |
+
payload = {
|
| 20 |
+
"model": "mixtral-8x7b-32768",
|
| 21 |
+
"messages": [
|
| 22 |
+
{
|
| 23 |
+
"role": "system",
|
| 24 |
+
"content": """You are a conversation generator for HR-employee interactions.
|
| 25 |
+
Generate realistic conversations with emotional context and natural flow.
|
| 26 |
+
Output should be in JSON format with the following structure for each turn:
|
| 27 |
+
{"role": "employee/hr", "message": "text", "emotion": "emotion_name"}"""
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"role": "user",
|
| 31 |
+
"content": prompt
|
| 32 |
+
}
|
| 33 |
+
],
|
| 34 |
+
"temperature": 0.7,
|
| 35 |
+
"max_tokens": 1000
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = httpx.post(
|
| 40 |
+
self.base_url,
|
| 41 |
+
headers=self.headers,
|
| 42 |
+
json=payload,
|
| 43 |
+
timeout=30.0
|
| 44 |
+
)
|
| 45 |
+
response.raise_for_status()
|
| 46 |
+
return response.json()['choices'][0]['message']['content']
|
| 47 |
+
except Exception as e:
|
| 48 |
+
st.error(f"Error calling Groq API: {e}")
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
def generate_conversation(self, scenario: str) -> List[Dict]:
|
| 52 |
+
prompt = f"""
|
| 53 |
+
Generate a realistic HR-employee conversation about the following scenario:
|
| 54 |
+
{scenario}
|
| 55 |
+
|
| 56 |
+
The conversation should:
|
| 57 |
+
1. Include natural emotional responses from the employee
|
| 58 |
+
2. Show professional and empathetic responses from HR
|
| 59 |
+
3. Have a natural flow and progression
|
| 60 |
+
4. Include 12-15 turns between the employee and HR
|
| 61 |
+
|
| 62 |
+
Return the conversation in JSON format as a list of messages, where each message has:
|
| 63 |
+
- role (employee/hr)
|
| 64 |
+
- message (the actual text)
|
| 65 |
+
- emotion (for employee messages only)
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
response = self._call_groq_api(prompt)
|
| 69 |
+
if response:
|
| 70 |
+
try:
|
| 71 |
+
response = response.replace("```json", "").replace("```", "").strip()
|
| 72 |
+
return json.loads(response)
|
| 73 |
+
except json.JSONDecodeError as e:
|
| 74 |
+
st.error(f"Error parsing JSON response: {e}")
|
| 75 |
+
return None
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
def generate_dataset(self, scenarios: List[str]) -> pd.DataFrame:
|
| 79 |
+
all_turns = []
|
| 80 |
+
|
| 81 |
+
for scenario_idx, scenario in enumerate(scenarios, 1):
|
| 82 |
+
with st.spinner(f'Generating conversation for scenario {scenario_idx}...'):
|
| 83 |
+
conversation = self.generate_conversation(scenario)
|
| 84 |
+
if conversation:
|
| 85 |
+
for turn in conversation:
|
| 86 |
+
all_turns.append({
|
| 87 |
+
'conversation_id': scenario_idx,
|
| 88 |
+
'role': turn['role'],
|
| 89 |
+
'message': turn['message'],
|
| 90 |
+
'emotion': turn.get('emotion', 'N/A'),
|
| 91 |
+
'scenario': scenario
|
| 92 |
+
})
|
| 93 |
+
time.sleep(1) # Small delay between API calls
|
| 94 |
+
|
| 95 |
+
if all_turns:
|
| 96 |
+
return pd.DataFrame(all_turns)
|
| 97 |
+
return None
|
| 98 |
+
|
| 99 |
+
def main():
|
| 100 |
+
st.title("HR Conversation Dataset Generator")
|
| 101 |
+
st.write("Generate realistic HR-employee conversations based on different scenarios.")
|
| 102 |
+
|
| 103 |
+
# API Key input
|
| 104 |
+
api_key = st.text_input("Enter your Groq API Key:", type="password")
|
| 105 |
+
|
| 106 |
+
# Scenario input
|
| 107 |
+
st.subheader("Enter Scenarios")
|
| 108 |
+
st.write("Add scenarios for generating conversations. Each scenario will generate a unique conversation.")
|
| 109 |
+
|
| 110 |
+
# Initialize scenarios list in session state if it doesn't exist
|
| 111 |
+
if 'scenarios' not in st.session_state:
|
| 112 |
+
st.session_state.scenarios = [""]
|
| 113 |
+
|
| 114 |
+
# Function to add new scenario field
|
| 115 |
+
def add_scenario():
|
| 116 |
+
st.session_state.scenarios.append("")
|
| 117 |
+
|
| 118 |
+
# Function to remove scenario field
|
| 119 |
+
def remove_scenario(index):
|
| 120 |
+
st.session_state.scenarios.pop(index)
|
| 121 |
+
|
| 122 |
+
# Display scenario input fields
|
| 123 |
+
new_scenarios = []
|
| 124 |
+
for i, scenario in enumerate(st.session_state.scenarios):
|
| 125 |
+
col1, col2 = st.columns([6, 1])
|
| 126 |
+
with col1:
|
| 127 |
+
new_scenario = st.text_area(f"Scenario {i+1}", scenario, key=f"scenario_{i}")
|
| 128 |
+
new_scenarios.append(new_scenario)
|
| 129 |
+
with col2:
|
| 130 |
+
if i > 0: # Don't allow removing the first scenario
|
| 131 |
+
if st.button("Remove", key=f"remove_{i}"):
|
| 132 |
+
remove_scenario(i)
|
| 133 |
+
st.rerun()
|
| 134 |
+
|
| 135 |
+
st.session_state.scenarios = new_scenarios
|
| 136 |
+
|
| 137 |
+
if st.button("Add Another Scenario"):
|
| 138 |
+
add_scenario()
|
| 139 |
+
st.rerun()
|
| 140 |
+
|
| 141 |
+
# Generate button
|
| 142 |
+
if st.button("Generate Dataset"):
|
| 143 |
+
if not api_key:
|
| 144 |
+
st.error("Please enter your Groq API key.")
|
| 145 |
+
return
|
| 146 |
+
|
| 147 |
+
# Filter out empty scenarios
|
| 148 |
+
scenarios = [s for s in st.session_state.scenarios if s.strip()]
|
| 149 |
+
|
| 150 |
+
if not scenarios:
|
| 151 |
+
st.error("Please enter at least one scenario.")
|
| 152 |
+
return
|
| 153 |
+
|
| 154 |
+
generator = GroqHRGenerator(api_key)
|
| 155 |
+
df = generator.generate_dataset(scenarios)
|
| 156 |
+
|
| 157 |
+
if df is not None:
|
| 158 |
+
st.success("Dataset generated successfully!")
|
| 159 |
+
|
| 160 |
+
# Display the dataset
|
| 161 |
+
st.subheader("Generated Dataset")
|
| 162 |
+
st.dataframe(df)
|
| 163 |
+
|
| 164 |
+
# Download button
|
| 165 |
+
csv = df.to_csv(index=False)
|
| 166 |
+
st.download_button(
|
| 167 |
+
label="Download CSV",
|
| 168 |
+
data=csv,
|
| 169 |
+
file_name="hr_conversations.csv",
|
| 170 |
+
mime="text/csv"
|
| 171 |
+
)
|
| 172 |
+
else:
|
| 173 |
+
st.error("Failed to generate dataset. Please try again.")
|
| 174 |
+
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
main()
|