File size: 2,858 Bytes
b032256
 
3c6cec7
373bb0a
 
 
 
b032256
 
3c6cec7
c8a0486
 
 
 
 
 
 
 
 
 
 
 
 
b032256
3c6cec7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b032256
c8a0486
3c6cec7
c8a0486
3c6cec7
 
 
 
 
 
 
 
 
 
 
 
 
 
373bb0a
b032256
 
 
3c6cec7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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()