import gradio as gr import re from typing import List, Dict from datetime import datetime class MissionGenerator: def __init__(self): self.mission_counter = 1 self.keywords = { 'main': [ 'must', 'need to', 'primary', 'main goal', 'objective is to', 'task is to', 'have to', 'required to' ], 'secondary': [ 'optional', 'can also', 'bonus', 'additional', 'extra', 'if possible', 'could also', 'might also' ], 'alternative': [ 'alternatively', 'another way', 'or', 'instead', 'different approach', 'other option', 'different way' ] } def extract_objectives(self, text: str) -> List[Dict]: sentences = [s.strip() for s in re.split(r'[.!?]+', text) if s.strip()] objectives = [] for sentence in sentences: sentence_lower = sentence.lower() # Determine objective type based on keywords and sentence position obj_type = 'Main' # Default to Main for first sentence if no other indicators if any(kw in sentence_lower for kw in self.keywords['alternative']): obj_type = 'Alternative' elif any(kw in sentence_lower for kw in self.keywords['secondary']): obj_type = 'Secondary' elif any(kw in sentence_lower for kw in self.keywords['main']) or not objectives: obj_type = 'Main' # Clean up the sentence clean_sentence = sentence for keyword_list in self.keywords.values(): for kw in keyword_list: clean_sentence = re.sub(rf'\b{kw}\b', '', clean_sentence, flags=re.IGNORECASE) # Further cleanup clean_sentence = re.sub(r'^[\s,]+|[\s,]+$', '', clean_sentence) clean_sentence = clean_sentence.strip() if clean_sentence: # Create identifier from key words words = re.findall(r'\b\w+\b', clean_sentence.lower()) identifier = '_'.join(words[:2]) objectives.append({ 'type': obj_type, 'identifier': identifier, 'description': clean_sentence }) return objectives def generate_response(self, user_input: str) -> tuple[str, str]: objectives = self.extract_objectives(user_input) # Generate formatted output output = f"# M{self.mission_counter}\n" for obj in objectives: output += f"Add {obj['type']} {obj['identifier']}\n" output += f"- {obj['description']}\n" # Check for silent/suppress keywords if any(word in user_input.lower() for word in ['silently', 'quietly', 'without notification', "don't notify"]): output += "Dnd\n" output += "#\n" # Generate chat response chat_response = f"I've created {len(objectives)} objectives based on your description. " if objectives: types = [obj['type'] for obj in objectives] chat_response += f"This includes {', '.join(f'{t.lower()} objectives' for t in set(types))}. " chat_response += "Would you like to modify any of them?" return chat_response, output def create_gradio_interface(): generator = MissionGenerator() def process_input(user_input: str, history: List[Dict]) -> tuple[List[Dict], str]: chat_response, formatted_output = generator.generate_response(user_input) history.append({"role": "user", "content": user_input}) history.append({"role": "assistant", "content": chat_response}) return history, formatted_output with gr.Blocks() as interface: gr.Markdown(""" # Original War Mission Objective Generator Describe your mission scenario and I'll help you create formatted mission objectives. Try phrases like "The player must...", "They can optionally...", or "Alternatively..." """) chatbot = gr.Chatbot(height=400, type="messages") msg = gr.Textbox( label="Describe your mission scenario", placeholder="Describe what the player needs to do..." ) clear = gr.Button("Clear Conversation") formatted_output = gr.Textbox( label="Generated Mission Objectives", lines=10, placeholder="Mission objectives will appear here..." ) msg.submit(process_input, inputs=[msg, chatbot], outputs=[chatbot, formatted_output]) clear.click(lambda: ([], ""), outputs=[chatbot, formatted_output]) gr.Examples( examples=[ "The player must infiltrate the enemy base. They can optionally rescue prisoners along the way. Alternatively, they could create a diversion at the front gate.", "Protect the convoy until it reaches the destination. You can also secure supply drops if possible.", "Your main goal is to capture the strategic point. Alternatively, try negotiating with the local faction silently.", ], inputs=msg ) return interface # Launch the interface if __name__ == "__main__": iface = create_gradio_interface() iface.launch()