Rajut commited on
Commit
2cea8a5
·
verified ·
1 Parent(s): 971d717

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import torch
4
+ import csv
5
+ import re
6
+ import warnings
7
+
8
+ warnings.filterwarnings("ignore")
9
+
10
+ # Define a prompt template for Magicoder with placeholders for instruction and response.
11
+ MAGICODER_PROMPT = """You are an exceptionally intelligent coding assistant that consistently delivers accurate and reliable responses to user instructions.
12
+ @@ Instruction
13
+ {instruction}
14
+ @@ Response
15
+ """
16
+
17
+ # Create a text generation pipeline using the Magicoder model, text-generation task, bfloat16 torch data type and auto device mapping.
18
+ generator = pipeline(
19
+ model="ise-uiuc/Magicoder-S-DS-6.7B",
20
+ task="text-generation",
21
+ torch_dtype=torch.bfloat16,
22
+ device_map="auto",
23
+ )
24
+
25
+ # Function to generate response
26
+ def generate_response(instruction):
27
+ prompt = MAGICODER_PROMPT.format(instruction=instruction)
28
+ result = generator(prompt, max_length=2048, num_return_sequences=1, temperature=0.0)
29
+ response = result[0]["generated_text"]
30
+ response_start_index = response.find("@@ Response") + len("@@ Response")
31
+ response = response[response_start_index:].strip()
32
+ return response
33
+
34
+ # Function to append data to a CSV file
35
+ def save_to_csv(data, filename):
36
+ with open(filename, 'a', newline='') as csvfile:
37
+ writer = csv.writer(csvfile)
38
+ writer.writerow(data)
39
+
40
+ # Function to process user feedback
41
+ def process_output(correct_output):
42
+ if correct_output.lower() == 'yes':
43
+ feedback = st.text_input("Do you want to provide any feedback?")
44
+ save_to_csv(["Correct", feedback], 'output_ratings.csv')
45
+ else:
46
+ correct_code = st.text_area("Please enter the correct code:")
47
+ feedback = st.text_input("Any other feedback you want to provide:")
48
+ save_to_csv(["Incorrect", feedback, correct_code], 'output_ratings.csv')
49
+
50
+ # Streamlit app
51
+ def main():
52
+ st.title("Magicoder Assistant")
53
+
54
+ instruction = st.text_area("Enter your instruction here:")
55
+ if st.button("Generate Response"):
56
+ generated_response = generate_response(instruction)
57
+ st.text("Generated response:")
58
+ st.text(generated_response)
59
+
60
+ correct_output = st.radio("Is the generated output correct?", ("Yes", "No"))
61
+ process_output(correct_output)
62
+
63
+ if __name__ == "__main__":
64
+ main()