Spaces:
Running
on
Zero
Running
on
Zero
File size: 2,936 Bytes
426f305 20d9496 5f4fb45 426f305 101b827 426f305 20d9496 426f305 3440da7 426f305 3ca495f 426f305 3ca495f 5f4fb45 34e4bd2 426f305 3440da7 20d9496 3440da7 426f305 3ca495f 426f305 5f4fb45 426f305 90f73f5 426f305 90f73f5 20d9496 426f305 20d9496 426f305 |
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 |
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import spaces
import io
# Initialize the Qwen model and tokenizer
model_name = "Qwen/Qwen2.5-Coder-7B-Instruct"
#model_name = "Qwen/Qwen2.5-Coder-32B-Instruct"
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Function to generate README and documentation
@spaces.GPU
def generate_documentation(code_input):
prompt = f"Generate README for the following code:\n\n{code_input}"
messages = [
{"role": "system", "content": "You are ReadMe Forge, 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."},
{"role": "user", "content": prompt}
]
# Prepare inputs for the model
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
# Generate the documentation
generated_ids = model.generate(**model_inputs, max_new_tokens=4000)
generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
documentation = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(documentation)
return documentation
# Gradio interface
def process_code(code_input):
documentation = generate_documentation(code_input)
# Save the documentation to a README.md file
file_path = "README.md"
with open(file_path, 'w', encoding='utf-8') as readme_file:
readme_file.write(documentation)
return documentation, file_path
with gr.Blocks(css=".container { font-family: 'Roboto', sans-serif; } .btn-primary { background-color: #007bff; } .icon { margin-right: 10px; }") as app:
gr.Markdown("""
#Code Documentation Generator
Paste your code below, and the app will generate the Documentation for you.
""")
with gr.Row():
code_input = gr.Textbox(lines=10, label="Paste your code here", placeholder="Enter your code...", show_label=False, elem_classes="form-control")
with gr.Row():
generate_button = gr.Button("Generate README", elem_classes="btn btn-primary")
with gr.Row():
output_text = gr.Markdown(label="Generated README")
download_button = gr.File(label="Download README.md")
# Bind function to button click
generate_button.click(process_code, inputs=code_input, outputs=[output_text, download_button])
# Launch the Gradio app
app.launch()
|