Spaces:
Sleeping
Sleeping
File size: 4,212 Bytes
f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a bb21ec9 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 f9c9c0a 083fef2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import os
import streamlit as st
import traceback
from langgraph.graph import StateGraph, START, END
from langchain.schema import HumanMessage
from langchain_groq import ChatGroq
from langsmith import traceable
from typing import TypedDict
# Load API Keys (Set in Hugging Face Spaces)
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
LANGSMITH_API_KEY = os.getenv("LANGSMITH_API_KEY")
# Ensure API Keys are set
if not GROQ_API_KEY or not LANGSMITH_API_KEY:
st.error("β οΈ Please set GROQ_API_KEY and LANGSMITH_API_KEY in your environment variables.")
st.stop()
# Initialize Groq LLM
llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="llama3-8b-8192")
# Define State
class State(TypedDict):
code_snippet: str
review_comments: str
suggestions: str
documentation: str
test_cases: str
# Function to review the code
@traceable(name="Code Review")
def code_review(data):
code_snippet = data.get("code_snippet", "")
prompt = f"Review the following code and provide feedback:\n\n{code_snippet}"
response = llm([HumanMessage(content=prompt)])
return {"review_comments": response.content}
# Function to generate improvement suggestions
@traceable(name="Improvement Suggestions")
def improvement_suggestions(data):
review_comments = data.get("review_comments", "")
prompt = f"Based on this review feedback, suggest improvements:\n\n{review_comments}"
response = llm([HumanMessage(content=prompt)])
return {"suggestions": response.content}
# Function to generate documentation
@traceable(name="Code Documentation Generator")
def generate_documentation(data):
code_snippet = data.get("code_snippet", "")
prompt = f"Generate proper docstrings and inline comments for the following code:\n\n{code_snippet}"
response = llm([HumanMessage(content=prompt)])
return {"documentation": response.content}
# Function to generate test cases
@traceable(name="Test Case Suggestions")
def generate_test_cases(data):
code_snippet = data.get("code_snippet", "")
prompt = f"Based on the given code, generate appropriate unit test cases:\n\n{code_snippet}"
response = llm([HumanMessage(content=prompt)])
return {"test_cases": response.content}
# Create LangGraph Workflow
def make_code_review_graph():
"""Create a LangGraph workflow for automated code reviews"""
graph_workflow = StateGraph(State)
graph_workflow.add_node("code_review", code_review)
graph_workflow.add_node("improvement_suggestions", improvement_suggestions)
graph_workflow.add_node("generate_documentation", generate_documentation)
graph_workflow.add_node("generate_test_cases", generate_test_cases)
graph_workflow.add_edge(START, "code_review")
graph_workflow.add_edge("code_review", "improvement_suggestions")
graph_workflow.add_edge("improvement_suggestions", "generate_documentation")
graph_workflow.add_edge("generate_documentation", "generate_test_cases")
graph_workflow.add_edge("generate_test_cases", END)
return graph_workflow.compile()
# Streamlit UI
st.title("π AI-Powered Code Review with LangGraph & LangSmith")
st.write("Analyze and improve your code using AI-based feedback, suggestions, documentation, and test cases.")
# Input Field
code_snippet = st.text_area("π Paste your code snippet below:", height=200)
if st.button("π Review Code"):
if not code_snippet.strip():
st.warning("β οΈ Please enter a valid code snippet.")
else:
try:
review_agent = make_code_review_graph()
result = review_agent.invoke({"code_snippet": code_snippet})
# Display Results with Clear Formatting
st.subheader("π‘ Review Comments")
st.write(result["review_comments"])
st.subheader("π§ Suggested Improvements")
st.write(result["suggestions"])
st.subheader("π Generated Documentation")
st.code(result["documentation"], language="python")
st.subheader("π§ͺ Suggested Test Cases")
st.code(result["test_cases"], language="python")
except Exception as e:
st.error(f"β οΈ Error: {str(e)}")
st.text(traceback.format_exc())
|