bala00712200502 commited on
Commit
a93076a
·
verified ·
1 Parent(s): 6bd03a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -64
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
- 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(
@@ -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
- llm = ChatOpenAI(
30
- model_name=MODEL_NAME,
31
- temperature=temperature,
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)
43
- - System Design and Architecture
44
- You provide:
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(
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})