Spaces:
Build error
Build error
updae gemni api
Browse files
app.py
CHANGED
|
@@ -1,10 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
from crewai import Agent, Task, Crew, Process
|
| 4 |
from langchain_openai import ChatOpenAI
|
| 5 |
|
| 6 |
-
# ===
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# === Streamlit UI Config ===
|
| 10 |
st.set_page_config(
|
|
@@ -18,12 +32,12 @@ if "messages" not in st.session_state:
|
|
| 18 |
st.session_state.messages = []
|
| 19 |
|
| 20 |
# === Java Coding Assistant Function ===
|
| 21 |
-
def java_coding_assistant(query,
|
| 22 |
try:
|
| 23 |
llm = ChatOpenAI(
|
| 24 |
-
model_name=
|
| 25 |
temperature=temperature,
|
| 26 |
-
api_key=
|
| 27 |
)
|
| 28 |
|
| 29 |
# Main Java Expert Agent
|
|
@@ -35,7 +49,6 @@ def java_coding_assistant(query, model_name="gpt-4.1-nano-2025-04-14", temperatu
|
|
| 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
|
|
@@ -49,7 +62,6 @@ You provide:
|
|
| 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
|
|
@@ -73,11 +85,9 @@ Include:
|
|
| 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:
|
|
@@ -94,17 +104,10 @@ for message in st.session_state.messages:
|
|
| 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})
|
|
|
|
| 1 |
+
# .env file (create this in your project root)
|
| 2 |
+
# -----------------------------------------
|
| 3 |
+
# Your Gemini API key and model configuration
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# app.py
|
| 7 |
import os
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
import streamlit as st
|
| 10 |
from crewai import Agent, Task, Crew, Process
|
| 11 |
from langchain_openai import ChatOpenAI
|
| 12 |
|
| 13 |
+
# === Load environment variables ===
|
| 14 |
+
load_dotenv() # reads the .env file and loads variables into the environment
|
| 15 |
+
|
| 16 |
+
# === API Key & Model Config ===
|
| 17 |
+
API_KEY = os.getenv("API_KEY")
|
| 18 |
+
MODEL_NAME = os.getenv("MODEL_NAME", "gemini-1.5")
|
| 19 |
+
|
| 20 |
+
# Ensure the key is available for downstream libraries
|
| 21 |
+
os.environ["OPENAI_API_KEY"] = API_KEY
|
| 22 |
|
| 23 |
# === Streamlit UI Config ===
|
| 24 |
st.set_page_config(
|
|
|
|
| 32 |
st.session_state.messages = []
|
| 33 |
|
| 34 |
# === Java Coding Assistant Function ===
|
| 35 |
+
def java_coding_assistant(query, temperature=0.7):
|
| 36 |
try:
|
| 37 |
llm = ChatOpenAI(
|
| 38 |
+
model_name=MODEL_NAME,
|
| 39 |
temperature=temperature,
|
| 40 |
+
api_key=API_KEY
|
| 41 |
)
|
| 42 |
|
| 43 |
# Main Java Expert Agent
|
|
|
|
| 49 |
- Backend Development (Spring Boot, Hibernate, Microservices)
|
| 50 |
- Android Development (Java/Kotlin, Android SDK)
|
| 51 |
- System Design and Architecture
|
|
|
|
| 52 |
You provide:
|
| 53 |
1. Complete code solutions with syntax highlighting
|
| 54 |
2. Detailed explanations of concepts
|
|
|
|
| 62 |
task = Task(
|
| 63 |
description=f"""Provide a comprehensive solution for this Java-related query:
|
| 64 |
{query}
|
|
|
|
| 65 |
Include:
|
| 66 |
1. Problem analysis
|
| 67 |
2. Complete Java code solution with proper syntax
|
|
|
|
| 85 |
)
|
| 86 |
|
| 87 |
result = crew.kickoff()
|
|
|
|
| 88 |
# Format code blocks if present
|
| 89 |
if "```java" in result:
|
| 90 |
result = result.replace("```java", "```python") # For Streamlit syntax highlighting
|
|
|
|
| 91 |
return result
|
| 92 |
|
| 93 |
except Exception as e:
|
|
|
|
| 104 |
|
| 105 |
# Chat input
|
| 106 |
if prompt := st.chat_input("Ask your Java question (e.g., 'How to implement REST API in Spring Boot?'):"):
|
|
|
|
| 107 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
| 108 |
with st.chat_message("user"):
|
| 109 |
st.markdown(prompt)
|
|
|
|
|
|
|
| 110 |
with st.chat_message("assistant"):
|
| 111 |
response = java_coding_assistant(prompt)
|
| 112 |
st.markdown(response, unsafe_allow_html=True)
|
| 113 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|