Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from fpdf import FPDF
|
| 4 |
+
import torch
|
| 5 |
+
import spaces
|
| 6 |
+
|
| 7 |
+
# Initialize the Qwen model and tokenizer
|
| 8 |
+
model_name = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 11 |
+
|
| 12 |
+
# Function to generate README and documentation
|
| 13 |
+
@spaces.GPU
|
| 14 |
+
def generate_documentation(code_input):
|
| 15 |
+
prompt = f"Generate README and documentation for the following code:\n\n{code_input}"
|
| 16 |
+
|
| 17 |
+
messages = [
|
| 18 |
+
{"role": "system", "content": "You are CodeDocify, a highly efficient and intelligent assistant designed to analyze code and generate comprehensive, clear, and concise documentation. Your purpose is to help developers by producing well-structured README files and detailed explanations of their code. You aim to simplify complex code into easily understandable documentation, ensuring that your responses are accurate, professional, and easy to follow."},
|
| 19 |
+
{"role": "user", "content": prompt}
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
# Prepare inputs for the model
|
| 23 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 24 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 25 |
+
|
| 26 |
+
# Generate the documentation
|
| 27 |
+
generated_ids = model.generate(**model_inputs, max_new_tokens=512)
|
| 28 |
+
generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
|
| 29 |
+
documentation = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 30 |
+
|
| 31 |
+
return documentation
|
| 32 |
+
|
| 33 |
+
# Function to generate and download PDF
|
| 34 |
+
def create_pdf(documentation):
|
| 35 |
+
pdf = FPDF()
|
| 36 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 37 |
+
pdf.add_page()
|
| 38 |
+
pdf.set_font("Arial", size=12)
|
| 39 |
+
pdf.multi_cell(200, 10, documentation)
|
| 40 |
+
|
| 41 |
+
file_name = "/mnt/data/Generated_Documentation.pdf"
|
| 42 |
+
pdf.output(file_name)
|
| 43 |
+
|
| 44 |
+
return file_name
|
| 45 |
+
|
| 46 |
+
# Gradio interface
|
| 47 |
+
def process_code(code_input):
|
| 48 |
+
documentation = generate_documentation(code_input)
|
| 49 |
+
pdf_path = create_pdf(documentation)
|
| 50 |
+
return documentation, pdf_path
|
| 51 |
+
|
| 52 |
+
# Set up the Gradio app with Bootstrap, icons, and smiley
|
| 53 |
+
with gr.Blocks(css=".container { font-family: 'Roboto', sans-serif; } .btn-primary { background-color: #007bff; } .icon { margin-right: 10px; }") as app:
|
| 54 |
+
gr.Markdown("""
|
| 55 |
+
# :notebook_with_decorative_cover: Code Documentation Generator
|
| 56 |
+
|
| 57 |
+
Paste your code below, and the app will generate the README and detailed documentation for you.
|
| 58 |
+
The output will also be available for download as a PDF.
|
| 59 |
+
""")
|
| 60 |
+
|
| 61 |
+
with gr.Row():
|
| 62 |
+
code_input = gr.Textbox(lines=10, label="Paste your code here", placeholder="Enter your code...", show_label=False, elem_classes="form-control")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
generate_button = gr.Button(":sparkles: Generate Documentation", elem_classes="btn btn-primary")
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
output_text = gr.Textbox(label="Generated Documentation", lines=20, interactive=False)
|
| 69 |
+
download_pdf = gr.File(label="Download PDF", file_types=[".pdf"])
|
| 70 |
+
|
| 71 |
+
# Bind function to button click
|
| 72 |
+
generate_button.click(process_code, inputs=code_input, outputs=[output_text, download_pdf])
|
| 73 |
+
|
| 74 |
+
# Launch the Gradio app
|
| 75 |
+
app.launch()
|