bala00712200502 commited on
Commit
90888fc
Β·
verified Β·
1 Parent(s): 861dcd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -37
app.py CHANGED
@@ -1,9 +1,3 @@
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
@@ -11,28 +5,26 @@ 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(
25
- page_title="πŸ‘¨β€πŸ’» Java Coding Assistant",
26
- page_icon="β˜•",
27
  layout="wide"
28
  )
29
 
30
- # === Session State ===
31
  if "messages" not in st.session_state:
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,
@@ -40,11 +32,11 @@ def java_coding_assistant(query, temperature=0.7):
40
  api_key=API_KEY
41
  )
42
 
43
- # Main Java Expert Agent
44
  java_expert = Agent(
45
  role="Senior Java Full Stack Developer",
46
  goal="Provide expert-level Java solutions with clear explanations and best practices",
47
- backstory="""You are a Java expert with 10+ years of experience in:
 
48
  - Core Java (Collections, Multithreading, JDBC)
49
  - Backend Development (Spring Boot, Hibernate, Microservices)
50
  - Android Development (Java/Kotlin, Android SDK)
@@ -53,28 +45,32 @@ You provide:
53
  1. Complete code solutions with syntax highlighting
54
  2. Detailed explanations of concepts
55
  3. Best practices and optimization tips
56
- 4. Alternative approaches when applicable""",
 
57
  llm=llm,
58
  verbose=True
59
  )
60
 
61
- # Task for handling the query
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
68
  3. Explanation of key concepts
69
  4. Best practices
70
- 5. Potential edge cases""",
 
71
  agent=java_expert,
72
- expected_output="""A well-structured response containing:
 
73
  - Problem breakdown
74
  - Complete Java code with syntax highlighting
75
  - Detailed explanation
76
  - Best practices
77
- - Runtime complexity analysis (if applicable)"""
 
78
  )
79
 
80
  crew = Crew(
@@ -85,29 +81,31 @@ Include:
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:
94
- return f"❌ Error: {str(e)}"
95
 
96
- # === Streamlit UI ===
97
  st.title("β˜• Java Coding Assistant")
98
  st.caption("Ask me anything about Java backend, Android development, or coding problems")
99
 
100
- # Display chat history
101
- for message in st.session_state.messages:
102
- with st.chat_message(message["role"]):
103
- st.markdown(message["content"])
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})
 
 
 
 
 
 
 
 
1
  import os
2
  from dotenv import load_dotenv
3
  import streamlit as st
 
5
  from langchain_openai import ChatOpenAI
6
 
7
  # === Load environment variables ===
8
+ load_dotenv() # loads API_KEY and MODEL_NAME from .env
9
 
10
  # === API Key & Model Config ===
11
  API_KEY = os.getenv("API_KEY")
12
  MODEL_NAME = os.getenv("MODEL_NAME", "gemini-1.5")
13
+ os.environ["OPENAI_API_KEY"] = API_KEY # needed by langchain_openai
 
 
14
 
15
  # === Streamlit UI Config ===
16
  st.set_page_config(
17
+ page_title="β˜• Java Coding Assistant",
18
+ page_icon="β˜•",
19
  layout="wide"
20
  )
21
 
22
+ # Initialize chat history
23
  if "messages" not in st.session_state:
24
  st.session_state.messages = []
25
 
26
+ # Java Coding Assistant function
27
+ def java_coding_assistant(query: str, temperature: float = 0.7) -> str:
28
  try:
29
  llm = ChatOpenAI(
30
  model_name=MODEL_NAME,
 
32
  api_key=API_KEY
33
  )
34
 
 
35
  java_expert = Agent(
36
  role="Senior Java Full Stack Developer",
37
  goal="Provide expert-level Java solutions with clear explanations and best practices",
38
+ backstory="""
39
+ You are a Java expert with 10+ years of experience in:
40
  - Core Java (Collections, Multithreading, JDBC)
41
  - Backend Development (Spring Boot, Hibernate, Microservices)
42
  - Android Development (Java/Kotlin, Android SDK)
 
45
  1. Complete code solutions with syntax highlighting
46
  2. Detailed explanations of concepts
47
  3. Best practices and optimization tips
48
+ 4. Alternative approaches when applicable
49
+ """,
50
  llm=llm,
51
  verbose=True
52
  )
53
 
 
54
  task = Task(
55
+ description=f"""
56
+ Provide a comprehensive solution for this Java-related query:
57
  {query}
58
  Include:
59
  1. Problem analysis
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(
 
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
89
 
90
  except Exception as e:
91
+ return f"❌ Error: {e}"
92
 
93
+ # === Streamlit interface ===
94
  st.title("β˜• Java Coding Assistant")
95
  st.caption("Ask me anything about Java backend, Android development, or coding problems")
96
 
97
+ # Render chat history
98
+ for msg in st.session_state.messages:
99
+ with st.chat_message(msg["role"]):
100
+ st.markdown(msg["content"])
101
 
102
  # Chat input
103
+ if user_input := st.chat_input("Ask your Java question (e.g., 'How to implement REST API in Spring Boot?'):"):
104
+ st.session_state.messages.append({"role": "user", "content": user_input})
105
  with st.chat_message("user"):
106
+ st.markdown(user_input)
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})
111
+ ```