abnerzhang commited on
Commit
df8f622
·
1 Parent(s): 56f90a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -26
app.py CHANGED
@@ -3,30 +3,51 @@ import gradio as gr
3
  def generate_ielts_essay(prompt):
4
  # You may want to customize the max_length and other parameters according to your needs
5
  return "Hello " + prompt + "!!"
6
-
7
- def generate_or_grade(task, text):
8
- if task == "Generate":
9
- # Generate an essay
10
- # output = generator(text, max_length=500)[0]['generated_text']
11
- return "Hello " + text + "!!"
12
- elif task == "Grade":
13
- # Simple grading function. Replace with a more sophisticated essay grading algorithm.
14
- words = text.split()
15
- if len(words) > 200:
16
- return "Good"
17
- else:
18
- return "Poor"
19
-
20
- iface = gr.Interface(
21
- fn=generate_or_grade,
22
- inputs=[
23
- gr.inputs.Radio(["Generate", "Grade"], label="Task"),
24
- gr.inputs.Textbox(lines=10, placeholder="Enter text...")
25
- ],
26
- outputs="text",
27
- title="IELTS Essay Generator and Grader",
28
- description="This tool generates a response to an IELTS writing prompt using AI and provides a simple grading.",
29
- )
30
-
31
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
 
3
  def generate_ielts_essay(prompt):
4
  # You may want to customize the max_length and other parameters according to your needs
5
  return "Hello " + prompt + "!!"
6
+ def format_chat_prompt(message, band, chat_history, instruction):
7
+ prompt = f"System:{instruction}"
8
+ for turn in chat_history:
9
+ user_message, bot_message = turn
10
+ prompt = f"{prompt}\nUser: {user_message}\nAssistant: {bot_message}"
11
+ prompt = f"{prompt}\nUser: IELTS Writing Band {band} - {message}\nAssistant:"
12
+ return prompt
13
+
14
+ def respond(message, band, chat_history, instruction, temperature=0.7):
15
+ prompt = format_chat_prompt(message, band, chat_history, instruction)
16
+ chat_history = chat_history + [[message, ""]]
17
+ max_tokens = {6: 100, 7: 200, 8: 300, 9: 400}[int(band)] # Change these values as needed
18
+ stream = client.generate_stream(prompt,
19
+ max_new_tokens=max_tokens,
20
+ stop_sequences=["\nUser:", ""],
21
+ temperature=temperature)
22
+ acc_text = ""
23
+ for idx, response in enumerate(stream):
24
+ text_token = response.token.text
25
+
26
+ if response.details:
27
+ return
28
+
29
+ if idx == 0 and text_token.startswith(" "):
30
+ text_token = text_token[1:]
31
+
32
+ acc_text += text_token
33
+ last_turn = list(chat_history.pop(-1))
34
+ last_turn[-1] += acc_text
35
+ chat_history = chat_history + [last_turn]
36
+ yield "", chat_history
37
+ acc_text = ""
38
+
39
+ with gr.Blocks() as demo:
40
+ chatbot = gr.Chatbot(height=240)
41
+ msg = gr.Textbox(label="IELTS Writing Prompt")
42
+ band = gr.Radio([6, 7, 8, 9], label="Band")
43
+ with gr.Accordion(label="Advanced options",open=False):
44
+ system = gr.Textbox(label="System message", lines=2, value="A conversation between a user and an LLM-based AI assistant. The assistant writes IELTS articles of various bands.")
45
+ temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=1, value=0.7, step=0.1)
46
+ btn = gr.Button("Submit")
47
+ clear = gr.ClearButton(components=[msg, chatbot], value="Clear console")
48
+
49
+ btn.click(respond, inputs=[msg, band, chatbot, system], outputs=[msg, chatbot])
50
+ msg.submit(respond, inputs=[msg, band, chatbot, system], outputs=[msg, chatbot])
51
+ gr.close_all()
52
+ demo.queue().launch(share=True)
53