Spaces:
Build error
Build error
Upload codingagent.py
Browse files- codingagent.py +110 -0
codingagent.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from crewai import Agent, Task, Crew, Process
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
+
|
| 6 |
+
# === API Keys ===
|
| 7 |
+
os.environ["OPENAI_API_KEY"] = "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" # Replace with your actual key
|
| 8 |
+
|
| 9 |
+
# === Streamlit UI Config ===
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="π¨βπ» Java Coding Assistant",
|
| 12 |
+
page_icon="β",
|
| 13 |
+
layout="wide"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
# === Session State ===
|
| 17 |
+
if "messages" not in st.session_state:
|
| 18 |
+
st.session_state.messages = []
|
| 19 |
+
|
| 20 |
+
# === Java Coding Assistant Function ===
|
| 21 |
+
def java_coding_assistant(query, model_name="gpt-4.1-nano-2025-04-14", temperature=0.7):
|
| 22 |
+
try:
|
| 23 |
+
llm = ChatOpenAI(
|
| 24 |
+
model_name=model_name,
|
| 25 |
+
temperature=temperature,
|
| 26 |
+
api_key=os.getenv("OPENAI_API_KEY")
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Main Java Expert Agent
|
| 30 |
+
java_expert = Agent(
|
| 31 |
+
role="Senior Java Full Stack Developer",
|
| 32 |
+
goal="Provide expert-level Java solutions with clear explanations and best practices",
|
| 33 |
+
backstory="""You are a Java expert with 10+ years of experience in:
|
| 34 |
+
- Core Java (Collections, Multithreading, JDBC)
|
| 35 |
+
- Backend Development (Spring Boot, Hibernate, Microservices)
|
| 36 |
+
- Android Development (Java/Kotlin, Android SDK)
|
| 37 |
+
- System Design and Architecture
|
| 38 |
+
|
| 39 |
+
You provide:
|
| 40 |
+
1. Complete code solutions with syntax highlighting
|
| 41 |
+
2. Detailed explanations of concepts
|
| 42 |
+
3. Best practices and optimization tips
|
| 43 |
+
4. Alternative approaches when applicable""",
|
| 44 |
+
llm=llm,
|
| 45 |
+
verbose=True
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# Task for handling the query
|
| 49 |
+
task = Task(
|
| 50 |
+
description=f"""Provide a comprehensive solution for this Java-related query:
|
| 51 |
+
{query}
|
| 52 |
+
|
| 53 |
+
Include:
|
| 54 |
+
1. Problem analysis
|
| 55 |
+
2. Complete Java code solution with proper syntax
|
| 56 |
+
3. Explanation of key concepts
|
| 57 |
+
4. Best practices
|
| 58 |
+
5. Potential edge cases""",
|
| 59 |
+
agent=java_expert,
|
| 60 |
+
expected_output="""A well-structured response containing:
|
| 61 |
+
- Problem breakdown
|
| 62 |
+
- Complete Java code with syntax highlighting
|
| 63 |
+
- Detailed explanation
|
| 64 |
+
- Best practices
|
| 65 |
+
- Runtime complexity analysis (if applicable)"""
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
crew = Crew(
|
| 69 |
+
agents=[java_expert],
|
| 70 |
+
tasks=[task],
|
| 71 |
+
process=Process.sequential,
|
| 72 |
+
verbose=True
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
result = crew.kickoff()
|
| 76 |
+
|
| 77 |
+
# Format code blocks if present
|
| 78 |
+
if "```java" in result:
|
| 79 |
+
result = result.replace("```java", "```python") # For Streamlit syntax highlighting
|
| 80 |
+
|
| 81 |
+
return result
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"β Error: {str(e)}"
|
| 85 |
+
|
| 86 |
+
# === Streamlit UI ===
|
| 87 |
+
st.title("β Java Coding Assistant")
|
| 88 |
+
st.caption("Ask me anything about Java backend, Android development, or coding problems")
|
| 89 |
+
|
| 90 |
+
# Display chat history
|
| 91 |
+
for message in st.session_state.messages:
|
| 92 |
+
with st.chat_message(message["role"]):
|
| 93 |
+
st.markdown(message["content"])
|
| 94 |
+
|
| 95 |
+
# Chat input
|
| 96 |
+
if prompt := st.chat_input("Ask your Java question (e.g., 'How to implement REST API in Spring Boot?'):"):
|
| 97 |
+
# Add user message to chat history
|
| 98 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 99 |
+
|
| 100 |
+
# Display user message
|
| 101 |
+
with st.chat_message("user"):
|
| 102 |
+
st.markdown(prompt)
|
| 103 |
+
|
| 104 |
+
# Display assistant response
|
| 105 |
+
with st.chat_message("assistant"):
|
| 106 |
+
response = java_coding_assistant(prompt)
|
| 107 |
+
st.markdown(response, unsafe_allow_html=True)
|
| 108 |
+
|
| 109 |
+
# Add assistant response to chat history
|
| 110 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|