Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import traceback
|
| 4 |
+
from langgraph.graph import StateGraph, START, END
|
| 5 |
+
from langchain.schema import HumanMessage
|
| 6 |
+
from langchain_groq import ChatGroq
|
| 7 |
+
from langsmith import traceable
|
| 8 |
+
from typing import TypedDict
|
| 9 |
+
|
| 10 |
+
# Load API keys from environment variables
|
| 11 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 12 |
+
LANGSMITH_API_KEY = os.getenv("LANGSMITH_API_KEY")
|
| 13 |
+
|
| 14 |
+
os.environ["LANGCHAIN_TRACING_V2"] = "true"
|
| 15 |
+
os.environ["LANGCHAIN_API_KEY"] = LANGSMITH_API_KEY
|
| 16 |
+
|
| 17 |
+
# Initialize Groq LLM
|
| 18 |
+
llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="mixtral-8x7b-32768")
|
| 19 |
+
|
| 20 |
+
# Define State
|
| 21 |
+
class State(TypedDict):
|
| 22 |
+
code_snippet: str
|
| 23 |
+
review_comments: str
|
| 24 |
+
suggestions: str
|
| 25 |
+
documentation: str
|
| 26 |
+
test_cases: str
|
| 27 |
+
|
| 28 |
+
# Function to review the code
|
| 29 |
+
@traceable(name="Code Review")
|
| 30 |
+
def code_review(data):
|
| 31 |
+
code_snippet = data.get("code_snippet", "")
|
| 32 |
+
prompt = f"Review the following code and provide feedback:\n\n{code_snippet}"
|
| 33 |
+
response = llm([HumanMessage(content=prompt)])
|
| 34 |
+
return {"review_comments": response.content}
|
| 35 |
+
|
| 36 |
+
# Function to generate improvement suggestions
|
| 37 |
+
@traceable(name="Improvement Suggestions")
|
| 38 |
+
def improvement_suggestions(data):
|
| 39 |
+
review_comments = data.get("review_comments", "")
|
| 40 |
+
prompt = f"Based on this review feedback, suggest improvements:\n\n{review_comments}"
|
| 41 |
+
response = llm([HumanMessage(content=prompt)])
|
| 42 |
+
return {"suggestions": response.content}
|
| 43 |
+
|
| 44 |
+
# Function to generate documentation
|
| 45 |
+
@traceable(name="Code Documentation Generator")
|
| 46 |
+
def generate_documentation(data):
|
| 47 |
+
code_snippet = data.get("code_snippet", "")
|
| 48 |
+
prompt = f"Generate proper docstrings and inline comments for the following code:\n\n{code_snippet}"
|
| 49 |
+
response = llm([HumanMessage(content=prompt)])
|
| 50 |
+
return {"documentation": response.content}
|
| 51 |
+
|
| 52 |
+
# Function to generate test cases
|
| 53 |
+
@traceable(name="Test Case Suggestions")
|
| 54 |
+
def generate_test_cases(data):
|
| 55 |
+
code_snippet = data.get("code_snippet", "")
|
| 56 |
+
prompt = f"Based on the given code, generate appropriate unit test cases:\n\n{code_snippet}"
|
| 57 |
+
response = llm([HumanMessage(content=prompt)])
|
| 58 |
+
return {"test_cases": response.content}
|
| 59 |
+
|
| 60 |
+
# Create LangGraph Workflow
|
| 61 |
+
def make_code_review_graph():
|
| 62 |
+
"""Create a LangGraph workflow for automated code reviews"""
|
| 63 |
+
graph_workflow = StateGraph(State)
|
| 64 |
+
|
| 65 |
+
graph_workflow.add_node("code_review", code_review)
|
| 66 |
+
graph_workflow.add_node("improvement_suggestions", improvement_suggestions)
|
| 67 |
+
graph_workflow.add_node("generate_documentation", generate_documentation)
|
| 68 |
+
graph_workflow.add_node("generate_test_cases", generate_test_cases)
|
| 69 |
+
|
| 70 |
+
graph_workflow.add_edge(START, "code_review")
|
| 71 |
+
graph_workflow.add_edge("code_review", "improvement_suggestions")
|
| 72 |
+
graph_workflow.add_edge("improvement_suggestions", "generate_documentation")
|
| 73 |
+
graph_workflow.add_edge("generate_documentation", "generate_test_cases")
|
| 74 |
+
graph_workflow.add_edge("generate_test_cases", END)
|
| 75 |
+
|
| 76 |
+
return graph_workflow.compile()
|
| 77 |
+
|
| 78 |
+
# Streamlit UI
|
| 79 |
+
st.title("🛠 AI-Powered Code Review with LangSmith Debugging")
|
| 80 |
+
st.write("Automate your code reviews using Groq AI, LangGraph, and LangSmith.")
|
| 81 |
+
|
| 82 |
+
code_snippet = st.text_area("Paste your code snippet for review:", height=200)
|
| 83 |
+
|
| 84 |
+
if st.button("Review Code"):
|
| 85 |
+
if not code_snippet.strip():
|
| 86 |
+
st.warning("⚠️ Please enter a code snippet.")
|
| 87 |
+
else:
|
| 88 |
+
try:
|
| 89 |
+
review_agent = make_code_review_graph()
|
| 90 |
+
result = review_agent.invoke({"code_snippet": code_snippet})
|
| 91 |
+
|
| 92 |
+
st.subheader("💡 Review Comments")
|
| 93 |
+
st.text_area("", result.get("review_comments", ""), height=150)
|
| 94 |
+
|
| 95 |
+
st.subheader("🔧 Suggested Improvements")
|
| 96 |
+
st.text_area("", result.get("suggestions", ""), height=150)
|
| 97 |
+
|
| 98 |
+
st.subheader("📖 Generated Documentation")
|
| 99 |
+
st.text_area("", result.get("documentation", ""), height=150)
|
| 100 |
+
|
| 101 |
+
st.subheader("🧪 Suggested Test Cases")
|
| 102 |
+
st.text_area("", result.get("test_cases", ""), height=150)
|
| 103 |
+
except Exception as e:
|
| 104 |
+
st.error(f"⚠️ Error: {str(e)}\n{traceback.format_exc()}")
|