import gradio as gr # Lists of positive and negative words for simple sentiment analysis positive_words = {"good", "great", "excellent", "awesome", "happy", "love", "like", "fantastic", "positive", "amazing", "wonderful", "best"} negative_words = {"bad", "terrible", "poor", "hate", "dislike", "awful", "sad", "negative", "horrible", "worst", "dreadful"} def analyze_sentiment(text): """Analyze sentiment by counting positive and negative words.""" words = text.lower().split() pos_count = sum(word in positive_words for word in words) neg_count = sum(word in negative_words for word in words) if pos_count > neg_count: sentiment = "Positive" score = pos_count / (pos_count + neg_count) if (pos_count + neg_count) > 0 else 0 elif neg_count > pos_count: sentiment = "Negative" score = neg_count / (pos_count + neg_count) if (pos_count + neg_count) > 0 else 0 else: sentiment = "Neutral" score = 0.5 if (pos_count + neg_count) > 0 else 0.5 return sentiment, round(score, 2) # Predefined code templates for simple coding tasks code_templates = { "hello world": "def hello_world():\n print('Hello, world!')", "add two numbers": "def add(a, b):\n return a + b", "factorial": "def factorial(n):\n if n == 0:\n return 1\n else:\n return n * factorial(n-1)", "fibonacci": "def fibonacci(n):\n if n <= 1:\n return n\n else:\n return fibonacci(n-1) + fibonacci(n-2)", } def generate_code(task: str) -> str: """Generate a Python code snippet for common tasks based on keywords.""" task_lower = task.lower() for key, code in code_templates.items(): if key in task_lower: return code return "# Sorry, I can't generate code for that task yet." # Create separate interfaces for sentiment analysis and code generation sentiment_interface = gr.Interface( fn=analyze_sentiment, inputs=gr.Textbox(label="Enter text"), outputs=[gr.Textbox(label="Sentiment"), gr.Number(label="Score")], title="Rule-based Sentiment Analysis", description="A tiny AI model that performs simple sentiment analysis using a list of positive and negative words.", ) code_interface = gr.Interface( fn=generate_code, inputs=gr.Textbox(label="Describe the coding task"), outputs=gr.Textbox(label="Generated Code"), title="Simple Code Generation", description="A tiny AI model that generates Python code snippets for common tasks. This is a mystery code model built for agentic coding.", ) # Combine both interfaces into a tabbed interface for a better user experience demo = gr.TabbedInterface( [sentiment_interface, code_interface], tab_names=["Sentiment Analysis", "Code Generation"], ) if __name__ == "__main__": demo.launch()