Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,17 @@ import os
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import streamlit as st
|
| 4 |
from crewai import Agent, Task, Crew, Process
|
| 5 |
-
|
| 6 |
|
| 7 |
# === Load environment variables ===
|
| 8 |
-
load_dotenv() # loads API_KEY
|
| 9 |
|
| 10 |
-
# === API Key
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# === Streamlit UI Config ===
|
| 16 |
st.set_page_config(
|
|
@@ -26,63 +28,37 @@ if "messages" not in st.session_state:
|
|
| 26 |
# Java Coding Assistant function
|
| 27 |
def java_coding_assistant(query: str, temperature: float = 0.7) -> str:
|
| 28 |
try:
|
| 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 |
-
2. Complete Java code solution with proper syntax
|
| 61 |
-
3. Explanation of key concepts
|
| 62 |
-
4. Best practices
|
| 63 |
-
5. Potential edge cases
|
| 64 |
-
""",
|
| 65 |
-
agent=java_expert,
|
| 66 |
-
expected_output="""
|
| 67 |
-
A well-structured response containing:
|
| 68 |
-
- Problem breakdown
|
| 69 |
-
- Complete Java code with syntax highlighting
|
| 70 |
-
- Detailed explanation
|
| 71 |
-
- Best practices
|
| 72 |
-
- Runtime complexity analysis (if applicable)
|
| 73 |
-
"""
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
-
crew = Crew(
|
| 77 |
-
agents=[java_expert],
|
| 78 |
-
tasks=[task],
|
| 79 |
-
process=Process.sequential,
|
| 80 |
-
verbose=True
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
result = crew.kickoff()
|
| 84 |
-
|
| 85 |
-
# Replace java fences for Streamlit
|
| 86 |
if "```java" in result:
|
| 87 |
result = result.replace("```java", "```python")
|
| 88 |
return result
|
|
@@ -107,4 +83,4 @@ if user_input := st.chat_input("Ask your Java question (e.g., 'How to implement
|
|
| 107 |
with st.chat_message("assistant"):
|
| 108 |
answer = java_coding_assistant(user_input)
|
| 109 |
st.markdown(answer, unsafe_allow_html=True)
|
| 110 |
-
st.session_state.messages.append({"role": "assistant", "content": answer})
|
|
|
|
| 2 |
from dotenv import load_dotenv
|
| 3 |
import streamlit as st
|
| 4 |
from crewai import Agent, Task, Crew, Process
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
|
| 7 |
# === Load environment variables ===
|
| 8 |
+
load_dotenv() # loads API_KEY from .env
|
| 9 |
|
| 10 |
+
# === API Key Config ===
|
| 11 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 12 |
+
os.environ["GEMINI_API_KEY"] = GEMINI_API_KEY # Set for potential other uses
|
| 13 |
+
|
| 14 |
+
# Configure Gemini
|
| 15 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 16 |
|
| 17 |
# === Streamlit UI Config ===
|
| 18 |
st.set_page_config(
|
|
|
|
| 28 |
# Java Coding Assistant function
|
| 29 |
def java_coding_assistant(query: str, temperature: float = 0.7) -> str:
|
| 30 |
try:
|
| 31 |
+
# Initialize Gemini model
|
| 32 |
+
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 33 |
+
|
| 34 |
+
# Create prompt with the query
|
| 35 |
+
prompt = f"""
|
| 36 |
+
You are a Senior Java Full Stack Developer with 10+ years of experience.
|
| 37 |
+
Provide a comprehensive solution for this Java-related query:
|
| 38 |
+
{query}
|
| 39 |
+
|
| 40 |
+
Your response should include:
|
| 41 |
+
1. Problem analysis
|
| 42 |
+
2. Complete Java code solution with proper syntax
|
| 43 |
+
3. Explanation of key concepts
|
| 44 |
+
4. Best practices
|
| 45 |
+
5. Potential edge cases
|
| 46 |
+
|
| 47 |
+
Format your response with:
|
| 48 |
+
- Problem breakdown
|
| 49 |
+
- Complete Java code with syntax highlighting (use ```java markers)
|
| 50 |
+
- Detailed explanation
|
| 51 |
+
- Best practices
|
| 52 |
+
- Runtime complexity analysis (if applicable)
|
| 53 |
+
"""
|
| 54 |
+
|
| 55 |
+
# Generate response
|
| 56 |
+
response = model.generate_content(prompt)
|
| 57 |
+
|
| 58 |
+
# Get the text from the response
|
| 59 |
+
result = response.text
|
| 60 |
+
|
| 61 |
+
# Replace java fences for Streamlit if needed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
if "```java" in result:
|
| 63 |
result = result.replace("```java", "```python")
|
| 64 |
return result
|
|
|
|
| 83 |
with st.chat_message("assistant"):
|
| 84 |
answer = java_coding_assistant(user_input)
|
| 85 |
st.markdown(answer, unsafe_allow_html=True)
|
| 86 |
+
st.session_state.messages.append({"role": "assistant", "content": answer})
|