File size: 3,154 Bytes
ea9e2cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from scripts.github_analyzer import GitHubAnalyzer
from scripts.topic_list import TOPIC_LIST
from scripts.error_handler import ErrorHandler

analyzer = GitHubAnalyzer()
error_handler = ErrorHandler()

async def process_url(
    url: str,
    main_cat: str,
    sub_cat: str,
    use_gpu: bool
) -> tuple[str, str, str]:
    try:
        if not all([url, main_cat, sub_cat]):
            response = error_handler.handle_github_url_error(
                url,
                "Please select all categories"
            )
            return response.errors[0].message, "", ""

        analyzer.set_device("cuda" if use_gpu else "cpu")
        response = await analyzer.analyze_repository(url, main_cat, sub_cat)

        if not response.success:
            return response.errors[0].message, "", ""

        readme_topics = " ".join([
            f"#{topic['topic'].lower()} ({topic['score']:.2f})"
            for topic in response.data["readme_topics"]
        ])

        code_topics = " ".join([
            f"#{topic['topic'].lower()} ({topic['score']:.2f})"
            for topic in response.data["code_topics"]
        ])

        dependencies = " ".join([
            f"#{dep.lower()}"
            for dep in response.data["dependencies"]
        ])

        return readme_topics, code_topics, dependencies

    except Exception as e:
        response = error_handler.handle_topic_analysis_error(str(e))
        return response.errors[0].message, "", ""

def create_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# Enhanced GitHub Topic Generator")

        with gr.Row():
            url_input = gr.Textbox(
                label="GitHub URL",
                placeholder="Enter GitHub repository URL"
            )

        with gr.Row():
            main_category = gr.Dropdown(
                choices=[None] + list(TOPIC_LIST.keys()),
                label="Main Category",
                value=None
            )
            sub_category = gr.Dropdown(
                choices=[],
                label="Sub Category"
            )

        with gr.Row():
            use_gpu = gr.Checkbox(
                label="Use GPU (Check if you have CUDA-capable GPU)",
                value=False
            )

        with gr.Row():
            generate_btn = gr.Button("Generate Topics")

        with gr.Row():
            readme_topics = gr.Textbox(label="README Topics")
            code_topics = gr.Textbox(label="Code Analysis Topics")
            dependencies = gr.Textbox(label="Dependencies")

        def update_sub_category(main_cat):
            return gr.Dropdown(
                choices=list(TOPIC_LIST[main_cat].keys()) if main_cat else []
            )

        main_category.change(
            update_sub_category,
            inputs=main_category,
            outputs=sub_category
        )

        generate_btn.click(
            process_url,
            inputs=[url_input, main_category, sub_category, use_gpu],
            outputs=[readme_topics, code_topics, dependencies]
        )

    return demo

if __name__ == "__main__":
    demo = create_interface()
    demo.launch(share=True)