HimanshuGoyal2004 commited on
Commit
700f9a1
Β·
1 Parent(s): 8cdde00

created app

Browse files
Files changed (3) hide show
  1. README.md +86 -1
  2. app.py +333 -0
  3. requirements.txt +13 -0
README.md CHANGED
@@ -10,4 +10,89 @@ pinned: false
10
  license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  license: mit
11
  ---
12
 
13
+ # πŸ€– Self-Documentation Agent System
14
+
15
+ An AI-powered system that automatically analyzes, documents, and validates Python code using LangGraph workflows and Google's Gemini 2.5 Flash model.
16
+
17
+ ## πŸš€ Features
18
+
19
+ - **Intelligent Documentation Detection**: Automatically skips documentation if already present
20
+ - **Comprehensive Code Documentation**: Generates detailed docstrings, inline comments, and warnings
21
+ - **Automated Code Testing**: Executes code with various test scenarios and captures I/O behavior
22
+ - **Professional Output**: Clean, business-appropriate formatting without excessive emojis
23
+ - **Library Research**: Uses web search to understand unfamiliar libraries
24
+ - **Issue Identification**: Flags potential syntax errors, logic problems, and security risks
25
+
26
+ ## πŸ”§ How It Works
27
+
28
+ The system uses a simplified three-node LangGraph workflow:
29
+
30
+ 1. **Research Node**: Analyzes code structure, identifies libraries, and checks existing documentation
31
+ 2. **Document Node**: Adds comprehensive docstrings and inline comments using Google style
32
+ 3. **Analyze Node**: Executes code with various test scenarios and identifies potential issues
33
+
34
+ ## πŸ“‹ Requirements
35
+
36
+ To use this system, you'll need:
37
+
38
+ - **Google API Key**: For accessing the Gemini 2.5 Flash model
39
+ - Get it from [Google AI Studio](https://makersuite.google.com/app/apikey)
40
+ - **Tavily API Key**: For web search functionality during library research
41
+ - Get it from [Tavily](https://tavily.com/)
42
+
43
+ ## 🎯 Use Cases
44
+
45
+ - **Code Review**: Automatically document and analyze code before reviews
46
+ - **Legacy Code**: Add documentation to undocumented codebases
47
+ - **Learning**: Understand how code works through comprehensive analysis
48
+ - **Quality Assurance**: Identify potential issues and improvements
49
+ - **Documentation Standards**: Ensure consistent documentation across projects
50
+
51
+ ## πŸ› οΈ Technologies
52
+
53
+ - **LangGraph**: Workflow orchestration and state management
54
+ - **Google Gemini 2.5 Flash**: Advanced language model for code analysis
55
+ - **Tavily Search**: Web search for library research
56
+ - **Gradio**: Interactive web interface
57
+ - **Python AST**: Code structure analysis
58
+
59
+ ## πŸ“– Example Output
60
+
61
+ ### Input Code
62
+ ```python
63
+ def divide_numbers(a, b):
64
+ return a / b
65
+ ```
66
+
67
+ ### Documented Output
68
+ ```python
69
+ def divide_numbers(a, b):
70
+ """
71
+ Divides two numbers.
72
+
73
+ Args:
74
+ a (float or int): The numerator.
75
+ b (float or int): The denominator.
76
+
77
+ Returns:
78
+ float: The result of the division.
79
+
80
+ Raises:
81
+ ZeroDivisionError: If the denominator 'b' is zero.
82
+ """
83
+ # WARNING: This function will raise a ZeroDivisionError if 'b' is 0.
84
+ # For robust applications, consider adding a try-except block or a check for b == 0.
85
+ return a / b
86
+ ```
87
+
88
+ ### Analysis Results
89
+ - **Issues Found**: Division by zero risk
90
+ - **Test Results**: Execution with various inputs
91
+ - **Recommendations**: Add error handling
92
+ - **Usage Guidelines**: Implementation best practices
93
+
94
+ ## πŸ”’ Privacy & Security
95
+
96
+ - API keys are handled securely and not stored
97
+ - Code processing happens in isolated environments
98
+ - No data is permanently stored or shared
app.py ADDED
@@ -0,0 +1,333 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Self-Documentation Agent System - Gradio Demo
3
+ A Hugging Face Spaces demo for the LangGraph-based code documentation workflow
4
+ """
5
+
6
+ import os
7
+ import gradio as gr
8
+ import tempfile
9
+ import zipfile
10
+ from io import BytesIO
11
+ from typing import Dict, Tuple
12
+ import datetime
13
+
14
+ # Import the main workflow function
15
+ from main import run_documentation_workflow
16
+
17
+ def process_code(code_input: str, google_api_key: str, tavily_api_key: str) -> Tuple[str, str, str]:
18
+ """
19
+ Process the input code through the documentation workflow
20
+
21
+ Args:
22
+ code_input: Python code to analyze and document
23
+ google_api_key: Google API key for Gemini model
24
+ tavily_api_key: Tavily API key for web search
25
+
26
+ Returns:
27
+ Tuple of (documented_code, analysis_results, status_message)
28
+ """
29
+ if not code_input.strip():
30
+ return "", "", "❌ Please provide Python code to analyze"
31
+
32
+ if not google_api_key.strip():
33
+ return "", "", "❌ Please provide a Google API key. Get one from [Google AI Studio](https://makersuite.google.com/app/apikey)"
34
+
35
+ if not tavily_api_key.strip():
36
+ return "", "", "❌ Please provide a Tavily API key. Get one from [Tavily](https://tavily.com/)"
37
+
38
+ try:
39
+ # Set environment variables
40
+ os.environ["GOOGLE_API_KEY"] = google_api_key.strip()
41
+ os.environ["TAVILY_API_KEY"] = tavily_api_key.strip()
42
+
43
+ # Show processing status
44
+ status_msg = "πŸ”„ Processing your code through the documentation workflow..."
45
+
46
+ # Run the workflow
47
+ result = run_documentation_workflow(code_input)
48
+
49
+ # Read the generated files
50
+ documented_code = ""
51
+ analysis_results = ""
52
+
53
+ try:
54
+ with open("code.py", "r", encoding="utf-8") as f:
55
+ documented_code = f.read()
56
+ except FileNotFoundError:
57
+ documented_code = "# No documented code generated\n# The workflow may have encountered an issue"
58
+
59
+ try:
60
+ with open("analysis.txt", "r", encoding="utf-8") as f:
61
+ analysis_results = f.read()
62
+ except FileNotFoundError:
63
+ analysis_results = "No analysis results generated. The workflow may have encountered an issue."
64
+
65
+ # Create status message
66
+ libraries_count = len(result.get('libraries_used', []))
67
+ issues_count = len(result.get('issues_found', []))
68
+ tests_count = len(result.get('test_results', []))
69
+
70
+ status = f"""βœ… **Workflow completed successfully!**
71
+
72
+ πŸ“Š **Summary:**
73
+ - πŸ“š Libraries found: {libraries_count}
74
+ - ⚠️ Issues identified: {issues_count}
75
+ - πŸ§ͺ Test results: {tests_count}
76
+ - πŸ“ Status: {result.get('current_step', 'completed')}
77
+
78
+ πŸ’‘ **Next Steps:**
79
+ 1. Review the documented code on the left
80
+ 2. Check the analysis results on the right
81
+ 3. Address any issues or recommendations
82
+ 4. Test your code with the suggested improvements
83
+ """
84
+
85
+ return documented_code, analysis_results, status
86
+
87
+ except Exception as e:
88
+ error_details = str(e)
89
+ if "API key" in error_details.lower():
90
+ error_msg = f"❌ **API Key Error**: {error_details}\n\nPlease check that your API keys are valid and have the necessary permissions."
91
+ elif "quota" in error_details.lower() or "limit" in error_details.lower():
92
+ error_msg = f"❌ **Rate Limit Error**: {error_details}\n\nYou may have exceeded your API quota. Please try again later."
93
+ else:
94
+ error_msg = f"❌ **Processing Error**: {error_details}\n\nPlease check your code syntax and try again. If the issue persists, it may be a temporary service problem."
95
+
96
+ return "", "", error_msg
97
+
98
+ def create_download_file() -> str:
99
+ """Create a zip file with the generated outputs for download"""
100
+ try:
101
+ # Create a temporary zip file
102
+ zip_buffer = BytesIO()
103
+
104
+ with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
105
+ # Add code.py if it exists
106
+ if os.path.exists("code.py"):
107
+ zip_file.write("code.py", "documented_code.py")
108
+
109
+ # Add analysis.txt if it exists
110
+ if os.path.exists("analysis.txt"):
111
+ zip_file.write("analysis.txt", "analysis_results.txt")
112
+
113
+ # Add workflow diagram if it exists
114
+ if os.path.exists("workflow_diagram.png"):
115
+ zip_file.write("workflow_diagram.png", "workflow_diagram.png")
116
+
117
+ # Save to temporary file
118
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.zip')
119
+ temp_file.write(zip_buffer.getvalue())
120
+ temp_file.close()
121
+
122
+ return temp_file.name
123
+
124
+ except Exception as e:
125
+ print(f"Error creating download file: {e}")
126
+ return None
127
+
128
+ # Sample code for demonstration
129
+ SAMPLE_CODE = '''import math
130
+ import random
131
+
132
+ def calculate_area(shape, **kwargs):
133
+ if shape == "circle":
134
+ return math.pi * kwargs["radius"] ** 2
135
+ elif shape == "rectangle":
136
+ return kwargs["width"] * kwargs["height"]
137
+ else:
138
+ return 0
139
+
140
+ def divide_numbers(a, b):
141
+ return a / b
142
+
143
+ def process_list(items):
144
+ total = 0
145
+ for i in range(len(items)):
146
+ total += items[i] * 2
147
+ return total
148
+
149
+ class Calculator:
150
+ def __init__(self):
151
+ self.history = []
152
+
153
+ def add(self, a, b):
154
+ result = a + b
155
+ self.history.append(f"{a} + {b} = {result}")
156
+ return result
157
+
158
+ def divide(self, a, b):
159
+ return divide_numbers(a, b)
160
+
161
+ # Example usage
162
+ calc = Calculator()
163
+ result = calc.add(5, 3)
164
+ area = calculate_area("circle", radius=5)
165
+ division = calc.divide(10, 2)
166
+ items = [1, 2, 3, 4]
167
+ processed = process_list(items)
168
+ print(f"Results: {result}, {area:.2f}, {division}, {processed}")'''
169
+
170
+ # Create the Gradio interface
171
+ def create_interface():
172
+ """Create the Gradio interface for the documentation system"""
173
+
174
+ with gr.Blocks(
175
+ title="Self-Documentation Agent System",
176
+ theme=gr.themes.Soft(),
177
+ css="""
178
+ .gradio-container {
179
+ max-width: 1200px !important;
180
+ }
181
+ .code-output {
182
+ font-family: 'Courier New', monospace;
183
+ font-size: 12px;
184
+ }
185
+ """
186
+ ) as demo:
187
+
188
+ gr.Markdown("""
189
+ # πŸ€– Self-Documentation Agent System
190
+
191
+ An AI-powered system that automatically analyzes, documents, and validates Python code using LangGraph workflows and Google's Gemini 2.5 Flash model.
192
+
193
+ ## ✨ Features
194
+ - **🧠 Intelligent Documentation**: Adds comprehensive docstrings and comments
195
+ - **πŸ” Code Analysis**: Identifies issues and provides recommendations
196
+ - **πŸ§ͺ Automated Testing**: Executes code with various test scenarios
197
+ - **πŸ“‹ Professional Output**: Clean, business-appropriate formatting
198
+ - **🌐 Library Research**: Uses web search to understand unfamiliar libraries
199
+
200
+ ## πŸš€ How to Use
201
+ 1. **πŸ”‘ Enter API Keys**: Get your free keys from the links below
202
+ 2. **πŸ“ Add Your Code**: Paste Python code or use the sample
203
+ 3. **⚑ Process**: Click "Analyze & Document Code"
204
+ 4. **πŸ“Š Review Results**: Check documented code and analysis
205
+
206
+ > **πŸ’‘ Tip**: The system works best with functions, classes, and complete code snippets!
207
+ """)
208
+
209
+ with gr.Row():
210
+ with gr.Column(scale=1):
211
+ gr.Markdown("### πŸ”‘ API Configuration")
212
+ google_key = gr.Textbox(
213
+ label="πŸ”‘ Google API Key",
214
+ placeholder="Enter your Google API key for Gemini access",
215
+ type="password",
216
+ info="πŸ”— Get your free key: https://makersuite.google.com/app/apikey"
217
+ )
218
+ tavily_key = gr.Textbox(
219
+ label="πŸ” Tavily API Key",
220
+ placeholder="Enter your Tavily API key for web search",
221
+ type="password",
222
+ info="πŸ”— Get your free key: https://tavily.com/"
223
+ )
224
+
225
+ gr.Markdown("### πŸ“ Input Code")
226
+ code_input = gr.Code(
227
+ label="Python Code to Analyze",
228
+ language="python",
229
+ value=SAMPLE_CODE,
230
+ lines=20,
231
+ info="Paste your Python code here for analysis and documentation"
232
+ )
233
+
234
+ analyze_btn = gr.Button(
235
+ "πŸš€ Analyze & Document Code",
236
+ variant="primary",
237
+ size="lg"
238
+ )
239
+
240
+ # Add sample code button
241
+ sample_btn = gr.Button(
242
+ "πŸ“‹ Load Sample Code",
243
+ variant="secondary"
244
+ )
245
+
246
+ with gr.Row():
247
+ status_output = gr.Markdown(
248
+ label="Status",
249
+ value="Ready to analyze code. Please provide your API keys and code."
250
+ )
251
+
252
+ with gr.Row():
253
+ with gr.Column(scale=1):
254
+ gr.Markdown("### πŸ“š Documented Code")
255
+ documented_output = gr.Code(
256
+ label="Generated Documentation",
257
+ language="python",
258
+ lines=25,
259
+ elem_classes=["code-output"],
260
+ info="Your code enhanced with comprehensive documentation"
261
+ )
262
+
263
+ with gr.Column(scale=1):
264
+ gr.Markdown("### πŸ” Analysis Results")
265
+ analysis_output = gr.Textbox(
266
+ label="Code Analysis & Test Results",
267
+ lines=25,
268
+ elem_classes=["code-output"],
269
+ info="Detailed analysis, issues, and test results"
270
+ )
271
+
272
+ # Event handlers
273
+ analyze_btn.click(
274
+ fn=process_code,
275
+ inputs=[code_input, google_key, tavily_key],
276
+ outputs=[documented_output, analysis_output, status_output]
277
+ )
278
+
279
+ sample_btn.click(
280
+ fn=lambda: SAMPLE_CODE,
281
+ outputs=code_input
282
+ )
283
+
284
+ # Add information section
285
+ with gr.Accordion("ℹ️ About the Workflow", open=False):
286
+ gr.Markdown("""
287
+ ### Workflow Steps
288
+
289
+ 1. **Research Node**: Analyzes code structure, identifies libraries, and checks existing documentation
290
+ 2. **Document Node**: Adds comprehensive docstrings and inline comments using Google style
291
+ 3. **Analyze Node**: Executes code with various test scenarios and identifies potential issues
292
+ 4. **Final Node**: Saves results and provides summary
293
+
294
+ ### What You Get
295
+
296
+ - **Enhanced Code**: Your original code with professional documentation
297
+ - **Detailed Analysis**: Comprehensive testing results and recommendations
298
+ - **Issue Identification**: Potential problems and improvement suggestions
299
+ - **Usage Guidelines**: Instructions for implementation and testing
300
+
301
+ ### API Keys Required
302
+
303
+ - **Google API Key**: Get it from [Google AI Studio](https://makersuite.google.com/app/apikey)
304
+ - **Tavily API Key**: Get it from [Tavily](https://tavily.com/) for web search functionality
305
+ """)
306
+
307
+ with gr.Accordion("πŸ”§ Technical Details", open=False):
308
+ gr.Markdown("""
309
+ ### Technologies Used
310
+
311
+ - **LangGraph**: Workflow orchestration and state management
312
+ - **Google Gemini 2.5 Flash**: Advanced language model for analysis
313
+ - **Tavily Search**: Web search for library research
314
+ - **Python AST**: Code structure analysis
315
+ - **Code Execution Tools**: Safe code testing environment
316
+
317
+ ### Supported Features
318
+
319
+ - Function and class documentation
320
+ - Inline code comments
321
+ - Error handling analysis
322
+ - Performance recommendations
323
+ - Security issue identification
324
+ - Test case generation
325
+ - I/O behavior documentation
326
+ """)
327
+
328
+ return demo
329
+
330
+ # Create and launch the interface
331
+ if __name__ == "__main__":
332
+ demo = create_interface()
333
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ langgraph
3
+ langchain
4
+ langchain-google-genai
5
+ langchain-community
6
+ langchain-experimental
7
+ tavily-python
8
+ python-dotenv
9
+ pyppeteer
10
+ pydantic
11
+ PyYAML
12
+ graphviz
13
+ requests