Rajut/CodeGeneration
0
1import streamlit as st2from transformers import pipeline3import csv4import re5import torch6import warnings7 8warnings.filterwarnings("ignore")9 10# Define a prompt template for Magicoder with placeholders for instruction and response.11MAGICODER_PROMPT = """You are an exceptionally intelligent coding assistant that consistently delivers accurate and reliable responses to user instructions.12@@ Instruction13{instruction}14@@ Response15"""16 17@st.cache(allow_output_mutation=True)18def load_model():19 return pipeline(20 model="ise-uiuc/Magicoder-S-DS-6.7B",21 task="text-generation"22 )23 24# Function to generate response25def generate_response(instruction):26 prompt = MAGICODER_PROMPT.format(instruction=instruction)27 result = model(prompt, max_length=2048, num_return_sequences=1, temperature=0.0)28 response = result[0]["generated_text"]29 response_start_index = response.find("@@ Response") + len("@@ Response")30 response = response[response_start_index:].strip()31 return response32 33# Function to append data to a CSV file34def save_to_csv(data, filename):35 with open(filename, 'a', newline='') as csvfile:36 writer = csv.writer(csvfile)37 writer.writerow(data)38 39# Streamlit app40def main():41 global model42 st.title("Magicoder Assistant")43 44 if 'model' not in globals():45 model = load_model()46 47 instruction = st.text_area("Enter your instruction here:")48 if st.button("Generate Response"):49 generated_response = generate_response(instruction)50 st.text("Generated response:")51 st.text(generated_response)52 53 correct_output = st.radio("Is the generated output correct?", ("Yes", "No"))54 if correct_output.lower() == 'yes':55 feedback = st.text_input("Do you want to provide any feedback?")56 save_to_csv(["Correct", feedback], 'output_ratings.csv')57 else:58 correct_code = st.text_area("Please enter the correct code:")59 feedback = st.text_input("Any other feedback you want to provide:")60 save_to_csv(["Incorrect", feedback, correct_code], 'output_ratings.csv')61 62if __name__ == "__main__":63 main()64 