File size: 2,364 Bytes
8b57e03
 
 
 
 
 
 
 
 
2b0ea98
8b57e03
 
 
 
55ae524
8b57e03
 
 
55ae524
8b57e03
 
 
55ae524
2b0ea98
8b57e03
 
 
 
 
 
 
 
 
 
 
 
 
 
55ae524
8b57e03
 
 
55ae524
 
 
8b57e03
 
 
 
 
 
 
 
 
 
 
 
55ae524
 
8b57e03
 
 
55ae524
8b57e03
55ae524
8b57e03
 
 
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
import streamlit as st
from st_utils import (
    load_tokenizer_and_model,
    generate_docstring,
    download_model,
    # list_files,
)
from huggingface_hub import hf_hub_download
import os
import torch

# list_files(os.getcwd())

# Set the title and description of the app
st.title("Code Function Summarization App")
st.write(
    """
This app uses the Hugging Face transformers library to generate summaries of input text. 
Simply select one of the sample Python functions from the dropdown menu below, and click the 'Summarize' button to generate a summary for the corresponding function.
"""
)

# st.write(f"Has CUDA: {torch.cuda.is_available()}")

# Download the model from the Hugging Face Hub if it doesn't exist
download_model()

# load the tokenizer and model
tokenizer, model, device = load_tokenizer_and_model("./models/pytorch_model.bin")

# Create a dropdown menu for the user to select a sample Python function
values = [
    "",
    "def multiply(a, b):\n    return a * b",
    "def get_data():\n    data = []\n    for i in range(10):\n        data.append(i)\n    return data",
    "def search(data, target):\n    for i in range(len(data)):\n        if data[i] == target:\n            return i\n    return -1",
]

selected_value = st.selectbox("Select a sample Python function:", values)

# Create a text input area for the user to enter their text
text_input = st.text_area(
    "Or enter your Python function here (prioritize this over the dropdown menu):",
    height=256,
    value=selected_value,
)


# Define a function to generate a summary
def generate_summary(text):
    summary = generate_docstring(model, tokenizer, device, text, max_length=30)
    return summary


# When the user clicks the 'Summarize' button, generate a summary
if st.button("Summarize") and (len(selected_value) > 0 or len(text_input) > 0):
    with st.spinner("Generating summary..."):
        if len(text_input) > 0:
            summaries = generate_summary(text_input)
            st.subheader("Docstrings:")
            for i, summary in enumerate(summaries):
                st.write(f"{i + 1}. " + summary)
        # if len(selected_value) > 0:
        else:
            summaries = generate_summary(selected_value)
            st.subheader("Docstrings:")
            for i, summary in enumerate(summaries):
                st.write(f"{i + 1}. " + summary)