Rajut commited on
Commit
288b3f7
β€’
1 Parent(s): 766a974

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
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, feedback=None, correct_code=None):
42
+ if correct_output.lower() == 'yes':
43
+ save_to_csv(["Correct", feedback], 'output_ratings.csv')
44
+ else:
45
+ save_to_csv(["Incorrect", feedback, correct_code], 'output_ratings.csv')
46
+
47
+ # Gradio interface
48
+ input_text = gr.inputs.Textbox(lines=5, label="Enter your instruction here:")
49
+ output_text = gr.outputs.Textbox(label="Generated response:")
50
+
51
+ def generate_and_process(instruction, correct_output):
52
+ generated_response = generate_response(instruction)
53
+ if correct_output.lower() == "no":
54
+ feedback = gr.Interface(
55
+ lambda x: x,
56
+ gr.inputs.Textbox(lines=5, label="Please enter the correct code:"),
57
+ "text"
58
+ ).launch()
59
+ correct_code = feedback
60
+ process_output(correct_output, feedback, correct_code)
61
+ else:
62
+ feedback = ""
63
+ process_output(correct_output, feedback)
64
+ return generated_response
65
+
66
+ title = "Magicoder Assistant"
67
+ description = "An intelligent coding assistant that generates responses based on user instructions."
68
+ examples = [["Implement a high-level API for a TODO list application. The API takes as input an operation request and updates the TODO list in place. If the request is invalid, raise an exception."]]
69
+
70
+ gr.Interface(
71
+ generate_and_process,
72
+ inputs=[input_text, "radio"],
73
+ outputs=output_text,
74
+ title=title,
75
+ description=description,
76
+ examples=examples
77
+ ).launch()