Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Set up the Streamlit app layout and title
|
5 |
+
st.title("CodeWise - AI-based Code Commenting Tool")
|
6 |
+
st.subheader("Paste your code below, and the AI will generate comments explaining it!")
|
7 |
+
|
8 |
+
# Create an input text area where users can paste their code
|
9 |
+
code_input = st.text_area("Paste your code here:", height=300)
|
10 |
+
|
11 |
+
# Slider to select the length of comments
|
12 |
+
comment_length = st.slider("Select the comment length", min_value=50, max_value=300, value=150, step=10)
|
13 |
+
|
14 |
+
# Create a button to generate the comments
|
15 |
+
if st.button("Generate Comments"):
|
16 |
+
if code_input.strip() == "":
|
17 |
+
st.warning("Please paste some code before generating comments.")
|
18 |
+
else:
|
19 |
+
# Load a pre-trained model from Hugging Face for code summarization
|
20 |
+
generator = pipeline("text2text-generation", model="Salesforce/codet5-base-multi-sum")
|
21 |
+
|
22 |
+
# Generate comments using the model
|
23 |
+
prompt = f"Comment this code:\n\n{code_input}"
|
24 |
+
result = generator(prompt, max_length=comment_length, num_return_sequences=1)
|
25 |
+
|
26 |
+
# Display the generated comments in the app
|
27 |
+
st.subheader("Generated Comments:")
|
28 |
+
st.code(result[0]['generated_text'], language='python')
|
29 |
+
|
30 |
+
# Display a footer message
|
31 |
+
st.write("Powered by [Hugging Face Transformers](https://huggingface.co/) and Streamlit.")
|