Reema-pp/Verbal_Communication_Trainer
0
1 2import os3import json4import random5import torch6import gradio as gr7from datetime import datetime8from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig9import whisper10 11# Global variables12chat_history = []13PROGRESS_FILE = "progress.json"14 15# Training data16training_data = {17 "storytelling": [18 "Tell a story about a challenge you overcame.",19 "Describe a time when you helped someone in need.",20 "Imagine a futuristic world and narrate a short story.",21 "Tell a story about a childhood memory that shaped you.",22 "Share an experience where you had to make a difficult choice.",23 "Narrate a funny or embarrassing moment and what you learned from it.",24 "Describe a time when a stranger impacted your life in an unexpected way.",25 "Tell a story about a travel experience that changed your perspective.",26 "Recount a time when you faced failure and how it helped you grow.",27 "Share a moment when you discovered something new about yourself."28 ],29 "impromptu_speaking": [30 "If you could invent a new technology, what would it be?",31 "Describe a moment that changed your life.",32 "What advice would you give to your younger self?",33 "If you could live in any historical period, which one and why?",34 "If you could have dinner with any historical figure, who would it be and why?",35 "What is one thing you would change about the world and why?",36 "If you won a million dollars today, what would you do with it?",37 "What is the most important skill for success in the modern world?",38 "Describe a book or movie that had a significant impact on you.",39 "If you had to teach a class on any subject, what would it be and why?"40 ],41 "conflict_resolution": [42 "A coworker takes credit for your work. How do you handle it?",43 "You have a disagreement with your manager. How do you resolve it?",44 "A team member is not contributing to a group project. What do you do?",45 "You need to deliver bad news to a client. How do you approach it?",46 "A customer is unhappy with your service. How do you handle the situation?",47 "You have a conflict with a close friend over a misunderstanding. How do you fix it?",48 "Two team members are in a heated argument during a meeting. How do you mediate?",49 "You are negotiating a deal, but the other party is being unreasonable. What is your approach?",50 "A family member disagrees with your life choices. How do you communicate effectively?",51 "A colleague keeps interrupting you during meetings. How do you address it professionally?"52 ]53}54 55def initialize_models():56 """Initialize AI models with error handling"""57 try:58 # Load Falcon-7B-Instruct with 4-bit Quantization59 model_name = "tiiuae/falcon-7b-instruct"60 61 quant_config = BitsAndBytesConfig(62 load_in_4bit=True,63 bnb_4bit_compute_dtype=torch.float1664 )65 66 tokenizer = AutoTokenizer.from_pretrained(model_name)67 if tokenizer.pad_token is None:68 tokenizer.pad_token = tokenizer.eos_token69 70 device = "cuda" if torch.cuda.is_available() else "cpu"71 72 model = AutoModelForCausalLM.from_pretrained(73 model_name,74 quantization_config=quant_config if device == "cuda" else None,75 device_map="auto" if device == "cuda" else None,76 torch_dtype=torch.float16 if device == "cuda" else torch.float3277 )78 79 # Load Whisper model80 whisper_model = whisper.load_model("small")81 82 print(f"✅ Models loaded successfully on: {device}")83 return model, tokenizer, whisper_model, device84 85 except Exception as e:86 print(f"❌ Error loading models: {e}")87 return None, None, None, "cpu"88 89# Initialize models90model, tokenizer, whisper_model, device = initialize_models()91 92def transcribe_audio(audio_file):93 """Convert speech to text"""94 if audio_file is None or whisper_model is None:95 return "❌ No audio file provided or Whisper model not loaded."96 97 try:98 result = whisper_model.transcribe(audio_file)99 return result["text"]100 except Exception as e:101 return f"❌ Error transcribing audio: {e}"102 103def generate_response(user_input):104 """Generate AI response with error handling"""105 if model is None or tokenizer is None:106 return "❌ AI model not loaded. Please try again later."107 108 try:109 formatted_input = f"User: {user_input}\nAssistant:"110 inputs = tokenizer(formatted_input, return_tensors="pt", truncation=True, max_length=512).to(device)111 112 with torch.no_grad():113 output = model.generate(114 **inputs,115 max_new_tokens=200,116 temperature=0.9,117 top_p=0.9,118 do_sample=True,119 pad_token_id=tokenizer.eos_token_id120 )121 122 response = tokenizer.decode(output[0], skip_special_tokens=True)123 124 if "Assistant:" in response:125 response = response.split("Assistant:")[-1].strip()126 127 return response128 129 except Exception as e:130 return f"❌ Error generating response: {e}"131 132def chat_with_ai(input_type, user_input, audio_input):133 """Handle chat with AI"""134 if input_type == "Voice":135 if audio_input is None:136 return "❌ No audio file provided. Please record your voice."137 user_input = transcribe_audio(audio_input)138 139 if not user_input or user_input.strip() == "":140 return "❌ Please provide some input."141 142 chat_history.append(f"User: {user_input}")143 144 if len(chat_history) > 10:145 chat_history.pop(0)146 147 response = generate_response(user_input)148 chat_history.append(f"Assistant: {response}")149 150 return response151 152def storytelling_prompt():153 """Pick a random storytelling prompt"""154 return random.choice(training_data["storytelling"])155 156def impromptu_speaking():157 """Pick a random topic for impromptu speaking"""158 return random.choice(training_data["impromptu_speaking"])159 160def conflict_resolution_scenario():161 """Pick a random conflict resolution scenario"""162 return random.choice(training_data["conflict_resolution"])163 164def evaluate_story(user_text):165 """Evaluate storytelling quality"""166 if not user_text or len(user_text.split()) < 5:167 return "❌ This input is too short to be a story. Please provide a complete story."168 169 prompt = f"""170You are a professional storytelling coach. Evaluate this story on:1711. Story Structure (1-10)1722. Emotional Engagement (1-10) 1733. Creativity & Originality (1-10)1744. Clarity & Flow (1-10)175 176Story: {user_text}177 178Provide scores and specific feedback for improvement.179"""180 181 return generate_response(prompt)182 183def evaluate_speech(user_text):184 """Evaluate impromptu speech"""185 if not user_text or len(user_text.split()) < 5:186 return "❌ This response is too short. Please provide a complete response."187 188 prompt = f"""189You are a professional speech coach. Evaluate this speech on:1901. Relevance to Topic (1-10)1912. Clarity & Structure (1-10)1923. Engagement & Persuasion (1-10)1934. Pacing & Confidence (1-10)194 195Speech: {user_text}196 197Provide scores and specific improvement tips.198"""199 200 return generate_response(prompt)201 202def assess_conflict_response(user_text):203 """Assess conflict resolution response"""204 if not user_text or len(user_text.split()) < 5:205 return "❌ This input is too short to assess. Please provide a full response."206 207 prompt = f"""208You are a conflict resolution expert. Evaluate this response on:2091. Empathy & Understanding (1-10)2102. Diplomacy & Professionalism (1-10)2113. Effectiveness of Resolution (1-10)212 213Response: {user_text}214 215Provide scores and actionable improvement tips.216"""217 218 return generate_response(prompt)219 220def save_progress_data(user_text, feedback, topic="General"):221 """Save user progress"""222 try:223 data = {224 "date": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),225 "topic": topic,226 "speech": user_text[:200] + "..." if len(user_text) > 200 else user_text,227 "feedback": feedback[:300] + "..." if len(feedback) > 300 else feedback228 }229 230 if os.path.exists(PROGRESS_FILE):231 with open(PROGRESS_FILE, "r") as f:232 progress_data = json.load(f)233 else:234 progress_data = []235 236 progress_data.append(data)237 238 # Keep only last 20 entries239 if len(progress_data) > 20:240 progress_data = progress_data[-20:]241 242 with open(PROGRESS_FILE, "w") as f:243 json.dump(progress_data, f, indent=2)244 245 return "✅ Progress saved!"246 247 except Exception as e:248 return f"❌ Error saving progress: {e}"249 250def load_progress():251 """Load user progress"""252 try:253 if not os.path.exists(PROGRESS_FILE):254 return "⚠️ No progress data found."255 256 with open(PROGRESS_FILE, "r") as f:257 progress_data = json.load(f)258 259 if not progress_data:260 return "⚠️ No past results available."261 262 summary = "📊 **Your Recent Performance:**\n\n"263 for entry in progress_data[-5:]:264 summary += f"📅 **Date:** {entry['date']}\n"265 summary += f"📝 **Topic:** {entry['topic']}\n"266 summary += f"💬 **Speech:** {entry['speech']}\n"267 summary += f"💡 **Feedback:** {entry['feedback']}\n\n"268 269 return summary270 271 except Exception as e:272 return f"❌ Error loading progress: {e}"273 274# Custom CSS275custom_css = """276body { 277 background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);278 font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;279}280 281.gradio-container {282 max-width: 1000px !important;283 margin: 0 auto !important;284 background: rgba(255, 255, 255, 0.95) !important;285 border-radius: 20px !important;286 box-shadow: 0 20px 40px rgba(0,0,0,0.1) !important;287}288 289.gr-button-primary {290 background: linear-gradient(45deg, #667eea, #764ba2) !important;291 border: none !important;292 border-radius: 10px !important;293 color: white !important;294 font-weight: bold !important;295 transition: all 0.3s ease !important;296}297 298.gr-button-primary:hover {299 transform: translateY(-2px) !important;300 box-shadow: 0 10px 20px rgba(0,0,0,0.2) !important;301}302"""303 304# Build Gradio Interface305with gr.Blocks(css=custom_css, title="AI Communication Trainer") as interface:306 gr.Markdown("# 🎤 AI Verbal Communication Trainer")307 gr.Markdown("### Improve your speaking skills with real-time AI feedback! 🚀")308 309 with gr.Tabs():310 with gr.Tab("🎯 Training Activities"):311 with gr.Accordion("🎲 Impromptu Speaking", open=True):312 impromptu_topic_btn = gr.Button("🎲 Generate Topic")313 impromptu_topic = gr.Textbox(label="Your Topic", interactive=False)314 315 impromptu_input_type = gr.Radio(["Text", "Voice"], label="Input Method", value="Text")316 impromptu_text = gr.Textbox(label="Your Speech", placeholder="Type your response here...")317 impromptu_audio = gr.Audio(label="🎙️ Record Speech", type="filepath")318 319 impromptu_submit = gr.Button("📊 Evaluate Speech", variant="primary")320 impromptu_feedback = gr.Textbox(label="AI Feedback", interactive=False, lines=8)321 322 impromptu_topic_btn.click(impromptu_speaking, outputs=impromptu_topic)323 impromptu_submit.click(324 lambda input_type, text, audio: evaluate_speech(325 transcribe_audio(audio) if input_type == "Voice" and audio else text326 ),327 inputs=[impromptu_input_type, impromptu_text, impromptu_audio],328 outputs=impromptu_feedback329 )330 331 with gr.Accordion("📖 Storytelling"):332 story_topic_btn = gr.Button("📖 Generate Story Prompt")333 story_topic = gr.Textbox(label="Your Story Prompt", interactive=False)334 335 story_input_type = gr.Radio(["Text", "Voice"], label="Input Method", value="Text")336 story_text = gr.Textbox(label="Your Story", placeholder="Tell your story here...")337 story_audio = gr.Audio(label="🎙️ Record Story", type="filepath")338 339 story_submit = gr.Button("📊 Evaluate Story", variant="primary")340 story_feedback = gr.Textbox(label="AI Feedback", interactive=False, lines=8)341 342 story_topic_btn.click(storytelling_prompt, outputs=story_topic)343 story_submit.click(344 lambda input_type, text, audio: evaluate_story(345 transcribe_audio(audio) if input_type == "Voice" and audio else text346 ),347 inputs=[story_input_type, story_text, story_audio],348 outputs=story_feedback349 )350 351 with gr.Accordion("🤝 Conflict Resolution"):352 conflict_topic_btn = gr.Button("🔥 Generate Scenario")353 conflict_topic = gr.Textbox(label="Your Scenario", interactive=False)354 355 conflict_input_type = gr.Radio(["Text", "Voice"], label="Input Method", value="Text")356 conflict_text = gr.Textbox(label="Your Response", placeholder="How would you handle this?")357 conflict_audio = gr.Audio(label="🎙️ Record Response", type="filepath")358 359 conflict_submit = gr.Button("📊 Evaluate Response", variant="primary")360 conflict_feedback = gr.Textbox(label="AI Feedback", interactive=False, lines=8)361 362 conflict_topic_btn.click(conflict_resolution_scenario, outputs=conflict_topic)363 conflict_submit.click(364 lambda input_type, text, audio: assess_conflict_response(365 transcribe_audio(audio) if input_type == "Voice" and audio else text366 ),367 inputs=[conflict_input_type, conflict_text, conflict_audio],368 outputs=conflict_feedback369 )370 371 with gr.Tab("💬 Chat with AI"):372 chat_input_type = gr.Radio(["Text", "Voice"], label="Input Method", value="Text")373 chat_text = gr.Textbox(label="Message", placeholder="Type your message...")374 chat_audio = gr.Audio(label="🎙️ Record Message", type="filepath")375 376 chat_submit = gr.Button("💬 Send", variant="primary")377 chat_output = gr.Textbox(label="AI Response", interactive=False, lines=6)378 379 chat_submit.click(380 chat_with_ai,381 inputs=[chat_input_type, chat_text, chat_audio],382 outputs=chat_output383 )384 385 with gr.Tab("📈 Progress"):386 gr.Markdown("## 📊 Track Your Improvement")387 388 progress_btn = gr.Button("📂 Load Progress")389 progress_output = gr.Textbox(label="Your Progress", interactive=False, lines=10)390 391 progress_btn.click(load_progress, outputs=progress_output)392 393if __name__ == "__main__":394 interface.launch(share=False)395 