This application is a Streamlit-based AI tool for generating programming code using Google's Gemini API. It provides context-aware code generation and explanations, allowing users to dynamically generate short-commented programming code based on their queries. The application uses conversational history to refine subsequent responses, making it highly interactive and conversationalβsimilar to ChatGPT.
- π§ Context-Aware Code Generation: Generates code based on user prompts and incorporates the context of previous interactions for improved accuracy.
- π Code Explanation: Provides a simple explanation of the generated code, focusing on functionality and key components.
- π Multi-Language Support: Supports popular programming languages, including Python, Java, C, and C++.
- π» Interactive User Interface: Built with Streamlit for a clean and responsive design.
- π Social Media Integration: Includes links to connect with the creator through LinkedIn, Instagram, and WhatsApp.
- π Streamlit: For creating the web application interface.
- π§© Google Generative AI: To generate programming code and explanations.
- π Python dotenv: For managing environment variables securely.
- π Python 3.8 or higher.
- π A valid Google API Key for accessing the Gemini model.
-
Clone the repository:
git clone https://github.com/your-username/your-repository.git cd your-repository -
Install the required dependencies:
pip install -r requirements.txt
-
Create a
.envfile in the root folder and add your Google API Key:GOOGLE_API_KEY=your_api_key_here -
Run the application locally:
streamlit run app.py
- π Open the application in your browser (default:
http://localhost:8501). - β¨οΈ Enter a coding task in the prompt area and select the programming language.
- π View generated code and explanations, along with your previous queries in the chat history.
- π If the prompt specifies "take input from user", the generated code will be interactive and accept user inputs dynamically.
- π app.py: Main application script.
- π requirements.txt: Lists all dependencies required to run the application.
- π .env: Stores environment variables (e.g.,
GOOGLE_API_KEY).
- β Add support for additional programming languages.
- π¨ Improve UI/UX with more responsive design elements.
- π¬ Enhance conversational context handling for complex queries.
Feel free to reach out or follow for updates:
- π LinkedIn: Connect with Vitthal Sawant
- πΈ Instagram: Follow on Instagram
- π± WhatsApp: Message on WhatsApp
- π§ Email: vitthalsawant300@gmail.com
Try out the application here: Code Generator AI
I'll analyze the code to explain the LLM model being used and other important aspects of your application.
Looking at your code.py file, I can see that you're using Google's Gemini AI model. Let me break down the key components:
model = genai.GenerativeModel(
model_name="gemini-2.0-flash",
generation_config=generation_config
)- You're using the
gemini-2.0-flashmodel, which is Google's latest generative AI model - This model is optimized for:
- Fast response times (hence "flash" in the name)
- Code generation and explanation
- Context-aware responses
- Handling multiple programming languages
generation_config = {
"temperature": 0.7,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 1024,
}temperature: 0.7 (controls creativity/randomness in responses)top_pandtop_k: 1 (controls diversity of responses)max_output_tokens: 1024 (maximum length of generated responses)
def generate_code_with_context(prompt, language, chat_history):
# Uses previous conversation history to maintain context
context = "\n".join([f"Q: {entry['prompt']}\nA: {entry['code']}" for entry in chat_history])- Maintains conversation history
- Generates code based on previous interactions
- Supports multiple programming languages (C, C++, Java, Python)
def explain_code_with_context(code, language, chat_history):
# Explains generated code in simple terms
explanation_prompt = f"Based on the previous conversation:\n{context}\nNow, explain this {language} code..."- Provides explanations for generated code
- Maintains context across explanations
- Focuses on main functionality and key components
- Clean Streamlit interface with sidebar navigation
- Social media integration (LinkedIn, Instagram, WhatsApp)
- Language selection dropdown
- Chat history display
- Code display with syntax highlighting
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = []
if 'input_key' not in st.session_state:
st.session_state['input_key'] = 0- Maintains conversation history across reruns
- Manages input field state
- Preserves user context
try:
# Code generation/explanation logic
except Exception as e:
return f"Error generating code: {str(e)}"- Graceful error handling for API calls
- User-friendly error messages
- Prevents application crashes
load_dotenv()
GOOGLE_API_KEY = os.getenv('GOOGLE_API_KEY')- Secure API key management using .env file
- Environment variable loading
- API key validation
streamlit>=1.28.0: Modern web interfacegoogle-generativeai>=0.3.0: Gemini AI integrationpython-dotenv>=1.0.0: Environment variable management- Additional UI enhancement packages:
streamlit-option-menu>=0.3.6streamlit-extras>=0.4.0pillow>=10.0.0(for image handling)watchdog>=3.0.0(for hot reloading)
This application is particularly useful for:
- Learning programming concepts
- Getting code explanations
- Generating code snippets with context
- Understanding code functionality
- Maintaining conversation history for better context
- Supporting multiple programming languages
- Providing a user-friendly interface for AI-powered code generation
The combination of Gemini AI with Streamlit creates a powerful, interactive coding assistant that can help users learn and understand code better while maintaining context across interactions.
Contributions are welcome and greatly appreciated! If you're interested in improving this project, here's how you can help:
- π΄ Fork the repository
- π§ Create your feature branch (
git checkout -b feature/amazing-feature) - π» Commit your changes (
git commit -m 'Add some amazing feature') - π€ Push to the branch (
git push origin feature/amazing-feature) - π Open a Pull Request
Whether you're fixing bugs, improving documentation, or adding new features, your contributions will make this project better for everyone! Feel free to reach out to Vitthal Sawant with any questions or ideas.
Let's build something amazing together! π