Spaces:
Sleeping
Sleeping
Add code generation feature for simple agentic coding tasks
Browse files
app.py
CHANGED
|
@@ -1,10 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
positive_words = {"good", "great", "
|
| 5 |
-
negative_words = {"bad", "terrible", "
|
| 6 |
|
| 7 |
def analyze_sentiment(text):
|
|
|
|
| 8 |
words = text.lower().split()
|
| 9 |
pos_count = sum(word in positive_words for word in words)
|
| 10 |
neg_count = sum(word in negative_words for word in words)
|
|
@@ -19,16 +20,45 @@ def analyze_sentiment(text):
|
|
| 19 |
score = 0.5 if (pos_count + neg_count) > 0 else 0.5
|
| 20 |
return sentiment, round(score, 2)
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
fn=analyze_sentiment,
|
| 24 |
inputs=gr.Textbox(label="Enter text"),
|
| 25 |
-
outputs=[
|
| 26 |
-
gr.Textbox(label="Sentiment"),
|
| 27 |
-
gr.Number(label="Score")
|
| 28 |
-
],
|
| 29 |
title="Rule-based Sentiment Analysis",
|
| 30 |
-
description="A tiny AI model that performs simple sentiment analysis using a list of positive and negative words."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
+
# Lists of positive and negative words for simple sentiment analysis
|
| 4 |
+
positive_words = {"good", "great", "excellent", "awesome", "happy", "love", "like", "fantastic", "positive", "amazing", "wonderful", "best"}
|
| 5 |
+
negative_words = {"bad", "terrible", "poor", "hate", "dislike", "awful", "sad", "negative", "horrible", "worst", "dreadful"}
|
| 6 |
|
| 7 |
def analyze_sentiment(text):
|
| 8 |
+
"""Analyze sentiment by counting positive and negative words."""
|
| 9 |
words = text.lower().split()
|
| 10 |
pos_count = sum(word in positive_words for word in words)
|
| 11 |
neg_count = sum(word in negative_words for word in words)
|
|
|
|
| 20 |
score = 0.5 if (pos_count + neg_count) > 0 else 0.5
|
| 21 |
return sentiment, round(score, 2)
|
| 22 |
|
| 23 |
+
# Predefined code templates for simple coding tasks
|
| 24 |
+
code_templates = {
|
| 25 |
+
"hello world": "def hello_world():\n print('Hello, world!')",
|
| 26 |
+
"add two numbers": "def add(a, b):\n return a + b",
|
| 27 |
+
"factorial": "def factorial(n):\n if n == 0:\n return 1\n else:\n return n * factorial(n-1)",
|
| 28 |
+
"fibonacci": "def fibonacci(n):\n if n <= 1:\n return n\n else:\n return fibonacci(n-1) + fibonacci(n-2)",
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def generate_code(task: str) -> str:
|
| 32 |
+
"""Generate a Python code snippet for common tasks based on keywords."""
|
| 33 |
+
task_lower = task.lower()
|
| 34 |
+
for key, code in code_templates.items():
|
| 35 |
+
if key in task_lower:
|
| 36 |
+
return code
|
| 37 |
+
# Fallback message when no template matches
|
| 38 |
+
return "# Sorry, I can't generate code for that task yet."
|
| 39 |
+
|
| 40 |
+
# Create separate interfaces for sentiment analysis and code generation
|
| 41 |
+
sentiment_interface = gr.Interface(
|
| 42 |
fn=analyze_sentiment,
|
| 43 |
inputs=gr.Textbox(label="Enter text"),
|
| 44 |
+
outputs=[gr.Textbox(label="Sentiment"), gr.Number(label="Score")],
|
|
|
|
|
|
|
|
|
|
| 45 |
title="Rule-based Sentiment Analysis",
|
| 46 |
+
description="A tiny AI model that performs simple sentiment analysis using a list of positive and negative words.",
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
code_interface = gr.Interface(
|
| 50 |
+
fn=generate_code,
|
| 51 |
+
inputs=gr.Textbox(label="Describe the coding task"),
|
| 52 |
+
outputs=gr.Textbox(label="Generated Code"),
|
| 53 |
+
title="Simple Code Generation",
|
| 54 |
+
description="A tiny AI model that generates Python code snippets for common tasks. This is a mystery code model built for agentic coding.",
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Combine both interfaces into a tabbed interface for a better user experience
|
| 58 |
+
demo = gr.TabbedInterface(
|
| 59 |
+
[sentiment_interface, code_interface],
|
| 60 |
+
tab_labels=["Sentiment Analysis", "Code Generation"],
|
| 61 |
)
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
demo.launch()
|