File size: 3,974 Bytes
c1a929e
3bef67a
 
bf25b85
 
c1a929e
 
a4853d5
 
 
 
 
 
 
 
 
 
 
 
 
 
c1a929e
2f82191
bf25b85
2f82191
bf25b85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cda9f17
bf25b85
cda9f17
bf25b85
5f65266
cda9f17
bf25b85
cda9f17
bf25b85
 
cda9f17
bf25b85
 
 
 
 
 
 
 
f0f5a18
bf25b85
 
 
 
 
 
c1a929e
 
 
 
 
bf25b85
6c877ff
bf25b85
 
 
 
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
112
import os
from prompts import *
import gradio as gr


def visualize_github_repo(repo_name, repo_branch, mistral_api_key):
    os.environ['MISTRAL_API_KEY'] = mistral_api_key

    from langgraph.graph import END, START, StateGraph
    from langchain_core.tracers.context import tracing_v2_enabled
    
    from utils import (
        load_github_codebase,
        router,
        get_plan_for_codebase,
        parse_plan,
        explore_file,
        final_mermaid_code_generation,
        extract_mermaid_and_generate_graph,
        GraphState,
    )
    
    yield "Looking at the Repo!"
    documents = load_github_codebase(repo_name, repo_branch)
    yield "Repo loaded!"

    # Define a new graph
    workflow = StateGraph(GraphState)

    # Define the two nodes we will cycle
    workflow.add_node("planner", get_plan_for_codebase)
    workflow.add_node("parse_plan", parse_plan)

    workflow.add_node("explore_file", explore_file)
    workflow.add_node("router", router)

    workflow.add_node("generate_mermaid_code", final_mermaid_code_generation)
    workflow.add_node("render_mermaid", extract_mermaid_and_generate_graph)

    # Set the entrypoint as `agent`
    # This means that this node is the first one called
    workflow.add_edge(START, "planner")
    workflow.add_edge("planner", "parse_plan")

    # We now add a conditional edge
    workflow.add_conditional_edges(
        # First, we define the start node. We use `agent`.
        # This means these are the edges taken after the `agent` node is called.
        "parse_plan",
        # Next, we pass in the function that will determine which node is called next.
        router,
    )
    workflow.add_conditional_edges(
        "explore_file",
        router,
    )
    workflow.add_edge("generate_mermaid_code", "render_mermaid")
    workflow.add_edge("render_mermaid", END)

    # Finally, we compile it!
    # This compiles it into a LangChain Runnable,
    # meaning you can use it as you would any other runnable.
    # Note that we're (optionally) passing the memory when compiling the graph
    app = workflow.compile()

    with tracing_v2_enabled():
        for s in app.stream(
            {"messages": [], "documents": documents},
            {"recursion_limit": 100},
        ):
            if "__end__" not in s:
                print(s)
                print("----")
                if "planner" in s:
                    yield "Planning the Exploration !"#, None)
                if "parse_plan" in s:
                    yield "Planning done! Parsing the plan!"#, None)
                if "explore_file" in s:
                    yield f"Exploration started! Exploring file: {s['explore_file']['explored_files'][-1]} !"

                if "generate_mermaid_code" in s:
                    yield "Exploration done! Gathering thoughts and generating a graph!",
                if "render_mermaid" in s:

                    yield s["render_mermaid"]["messages"][-1].content


demo = gr.Interface(
    fn=visualize_github_repo,
    inputs=[
        gr.Textbox(
            label="Repo Name",  # Input component label
            value="abhishekkrthakur/autoxgb",  # Input component placeholder
            placeholder="Name of the Public Repo in format author/repo",  # Input component description
        ),
        gr.Textbox(
            label="Repo Branch",  # Input component label
            value="main",  # Input component placeholder
            placeholder="Branch to explore",  # Input component description
        ),
        gr.Textbox(
            label="Mistral API Key",  # Input component label
            # value="main",  # Input component placeholder
            placeholder="Mistral API Key from mistral.ai",  # Input component description
        ),
    ],
    outputs = gr.Textbox(label="Mermaid Graph", placeholder="Visualization of the functionalities of the Repo",), # Output component description
    title="Repo Functionality Visualizer",
)

demo.launch()