Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,282 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
"""
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
prompt = f"""### Instruction:
|
| 7 |
Answer the provided question with the knowledge provided to you
|
| 8 |
### Question:
|
| 9 |
{instruction}
|
|
|
|
| 10 |
### Answer:
|
| 11 |
"""
|
| 12 |
-
return prompt
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
return f"{response}!"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
gr.Interface(fn=greet, inputs="text", outputs="text").launch(debug=True)
|
|
|
|
| 1 |
+
from unsloth import FastLanguageModel
|
| 2 |
+
import torch
|
| 3 |
import gradio as gr
|
| 4 |
+
model,tokenizer = FastLanguageModel.from_pretrained('./unified_model')
|
| 5 |
|
| 6 |
+
def generate_response_true_false(instruction):
|
| 7 |
+
"""
|
| 8 |
+
Generates a response using your fine-tuned model based on the provided instruction.
|
| 9 |
+
|
| 10 |
+
This function enables faster inference through the `FastLanguageModel` and prepares a
|
| 11 |
+
prompt for the model to determine whether the given statement is "True" or "False".
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
instruction (str): A string containing the statement and instructions to be evaluated.
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
str: "True" or "False" based on the model's response, or "Unable to determine" if the
|
| 18 |
+
response cannot be parsed reliably.
|
| 19 |
+
"""
|
| 20 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference within the function
|
| 21 |
+
prompt = f"""### Instruction:
|
| 22 |
+
Determine if the following statement is true or false. Respond only with "True" or "False".
|
| 23 |
+
|
| 24 |
+
### Statement:
|
| 25 |
+
{instruction}
|
| 26 |
+
|
| 27 |
+
### Answer:"""
|
| 28 |
+
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 32 |
+
|
| 33 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 34 |
+
response = response.split("### Answer:")[-1].strip()
|
| 35 |
+
|
| 36 |
+
# Extract True/False from response
|
| 37 |
+
if response.lower() == "true":
|
| 38 |
+
return "True"
|
| 39 |
+
elif response.lower() == "false":
|
| 40 |
+
return "False"
|
| 41 |
+
else:
|
| 42 |
+
# Try to identify the answer even if it's not perfectly formatted
|
| 43 |
+
if "true" in response.lower():
|
| 44 |
+
return "True"
|
| 45 |
+
elif "false" in response.lower():
|
| 46 |
+
return "False"
|
| 47 |
+
else:
|
| 48 |
+
return "Unable to determine."
|
| 49 |
+
|
| 50 |
+
def generate_response_open_ended(instruction):
|
| 51 |
+
"""
|
| 52 |
+
Generates a response using your fine-tuned model based on the provided instruction.
|
| 53 |
+
|
| 54 |
+
This function enables faster inference through the `FastLanguageModel` and prepares a
|
| 55 |
+
prompt for the model to determine whether the given statement is "True" or "False".
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
instruction (str): A string containing the statement and instructions to be evaluated.
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
str: A response from the model to the provided question or "Unable to determine" if the
|
| 62 |
+
response cannot be parsed reliably.
|
| 63 |
+
"""
|
| 64 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference within the function
|
| 65 |
prompt = f"""### Instruction:
|
| 66 |
Answer the provided question with the knowledge provided to you
|
| 67 |
### Question:
|
| 68 |
{instruction}
|
| 69 |
+
|
| 70 |
### Answer:
|
| 71 |
"""
|
|
|
|
| 72 |
|
| 73 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 74 |
+
with torch.no_grad():
|
| 75 |
+
outputs = model.generate(**inputs,early_stopping=False,min_length=50,length_penalty=2,max_length=300)
|
| 76 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 77 |
+
return response
|
| 78 |
+
|
| 79 |
+
def generate_response_multiple_choice(instruction,choice_A,choice_B,choice_C,choice_D):
|
| 80 |
+
"""
|
| 81 |
+
Generates a response using a fine-tuned language model for multiple-choice questions.
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
instruction (str): A string containing the question and its options.
|
| 85 |
+
|
| 86 |
+
Returns:
|
| 87 |
+
dict: A dictionary with the selected choice and its justification.
|
| 88 |
+
Example:
|
| 89 |
+
{
|
| 90 |
+
"choice": "A",
|
| 91 |
+
"justification": "Explanation for why Option A is correct."
|
| 92 |
+
}
|
| 93 |
+
If the model fails to provide a valid response, defaults to:
|
| 94 |
+
{
|
| 95 |
+
"choice": "None",
|
| 96 |
+
"justification": "Could not parse JSON"
|
| 97 |
+
}
|
| 98 |
+
"""
|
| 99 |
+
# Enable native faster inference for the model
|
| 100 |
+
FastLanguageModel.for_inference(model)
|
| 101 |
+
|
| 102 |
+
# Define the prompt with a detailed instruction for the model
|
| 103 |
+
prompt = f"""### Instruction:
|
| 104 |
+
In the following question, you are provided with 4 choices. Select the best choice based on the knowledge provided and provide a justification for that choice.
|
| 105 |
+
|
| 106 |
+
**You must return only your response with the following keys:**
|
| 107 |
+
- "choice": The best choice letter
|
| 108 |
+
- "justification": The justification for your choice
|
| 109 |
+
|
| 110 |
+
**Example Response:**
|
| 111 |
+
**choice**: A
|
| 112 |
+
**justification**: Explanation for why Option A is correct
|
| 113 |
+
|
| 114 |
+
### Question:
|
| 115 |
+
{instruction}
|
| 116 |
+
|
| 117 |
+
### Choices:
|
| 118 |
+
A) {choice_A}
|
| 119 |
+
B) {choice_B}
|
| 120 |
+
C) {choice_C}
|
| 121 |
+
D) {choice_D}
|
| 122 |
+
|
| 123 |
+
### Answer:
|
| 124 |
+
"""
|
| 125 |
+
|
| 126 |
+
# Tokenize the prompt and move it to GPU for inference
|
| 127 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 128 |
+
|
| 129 |
+
# Generate a response from the model
|
| 130 |
+
with torch.no_grad():
|
| 131 |
+
outputs = model.generate(
|
| 132 |
+
**inputs,
|
| 133 |
+
early_stopping=True,
|
| 134 |
+
min_length=50,
|
| 135 |
+
length_penalty=2,
|
| 136 |
+
do_sample=True,
|
| 137 |
+
max_new_tokens=300,
|
| 138 |
+
top_p=0.95,
|
| 139 |
+
top_k=50,
|
| 140 |
+
temperature=0.7,
|
| 141 |
+
num_return_sequences=1
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# Decode the response into text
|
| 145 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 146 |
+
return response
|
| 147 |
+
|
| 148 |
+
def true_false_greet(question):
|
| 149 |
+
if question == "":
|
| 150 |
+
# Return a default response if no input is given
|
| 151 |
+
return "No question was given to answer"
|
| 152 |
+
else:
|
| 153 |
+
# Call a placeholder function (must be implemented separately)
|
| 154 |
+
response = generate_response_true_false(question) # Note: This function is not defined in this code
|
| 155 |
+
return f"{response}!"
|
| 156 |
+
|
| 157 |
+
def open_ended_greet(question):
|
| 158 |
+
"""
|
| 159 |
+
Processes the user's question and returns a response.
|
| 160 |
+
|
| 161 |
+
Args:
|
| 162 |
+
question (str): The input text provided by the user.
|
| 163 |
+
|
| 164 |
+
Returns:
|
| 165 |
+
str: A processed response. If no input is given, a default message is returned.
|
| 166 |
+
"""
|
| 167 |
+
if question == "":
|
| 168 |
+
# Return a default response if no question is provided
|
| 169 |
+
return "No question was given to answer"
|
| 170 |
+
else:
|
| 171 |
+
# Call a placeholder function (must be implemented separately) to generate a response
|
| 172 |
+
response = generate_response_open_ended(question) # Note: generate_response is not defined in this snippet
|
| 173 |
+
|
| 174 |
+
# Extract the answer from the generated response by splitting on "### Answer:"
|
| 175 |
+
response = response.split('### Answer:')[1]
|
| 176 |
+
|
| 177 |
+
# Return the formatted response
|
| 178 |
+
return f"{response}!"
|
| 179 |
+
|
| 180 |
+
def multiple_choice_greet(question, choice_A, choice_B, choice_C, choice_D):
|
| 181 |
+
"""
|
| 182 |
+
Processes the user's question and multiple-choice options to generate a response.
|
| 183 |
+
|
| 184 |
+
Args:
|
| 185 |
+
question (str): The input question provided by the user.
|
| 186 |
+
choice_A (str): Option A for the question.
|
| 187 |
+
choice_B (str): Option B for the question.
|
| 188 |
+
choice_C (str): Option C for the question.
|
| 189 |
+
choice_D (str): Option D for the question.
|
| 190 |
+
|
| 191 |
+
Returns:
|
| 192 |
+
str: A response based on the input.
|
| 193 |
+
If no question is provided, returns a default message.
|
| 194 |
+
If no choices are provided, returns a default message.
|
| 195 |
+
"""
|
| 196 |
+
if question == "":
|
| 197 |
+
# Return a default response if no question is provided
|
| 198 |
+
return "No question was given to answer"
|
| 199 |
+
if choice_A == "" and choice_B == "" and choice_C == "" and choice_D == "":
|
| 200 |
+
# Return a default response if no choices are provided
|
| 201 |
+
return "No choice was given"
|
| 202 |
+
else:
|
| 203 |
+
# Call a placeholder function (must be implemented separately) to generate a response
|
| 204 |
+
response = generate_response_multiple_choice(question, choice_A, choice_B, choice_C, choice_D)
|
| 205 |
+
|
| 206 |
+
# Extract the answer from the generated response by splitting on "### Answer:"
|
| 207 |
+
response = response.split('### Answer:')[1]
|
| 208 |
+
|
| 209 |
+
# Return the formatted response
|
| 210 |
+
return f"{response}"
|
| 211 |
+
|
| 212 |
+
def show_true_false_interface():
|
| 213 |
+
return gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)
|
| 214 |
+
|
| 215 |
+
def show_open_ended_interface():
|
| 216 |
+
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
| 217 |
+
|
| 218 |
+
def show_multiple_choice_interface():
|
| 219 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
| 220 |
+
|
| 221 |
+
with gr.Blocks() as demo:
|
| 222 |
+
|
| 223 |
+
with gr.Row():
|
| 224 |
+
btn_t_f = gr.Button('True/False questions')
|
| 225 |
+
btn_open_ended = gr.Button('Open-Ended questions')
|
| 226 |
+
btn_m_c = gr.Button('Multiple-Choice questions')
|
| 227 |
+
|
| 228 |
+
with gr.Column(visible=True) as true_false_interface:
|
| 229 |
+
gr.Markdown("## True-False Template")
|
| 230 |
+
question_simple = gr.Textbox(label="Enter your question")
|
| 231 |
+
simple_output = gr.Textbox(label="Output", interactive=False)
|
| 232 |
+
submit_simple = gr.Button("Submit")
|
| 233 |
+
submit_simple.click(true_false_greet, inputs=question_simple, outputs=simple_output)
|
| 234 |
+
|
| 235 |
+
with gr.Column(visible=False) as open_ended_interface:
|
| 236 |
+
gr.Markdown("## Open Ended Template")
|
| 237 |
+
question_simple = gr.Textbox(label="Enter your question")
|
| 238 |
+
simple_output = gr.Textbox(label="Output", interactive=False)
|
| 239 |
+
submit_simple = gr.Button("Submit")
|
| 240 |
+
submit_simple.click(open_ended_greet, inputs=question_simple, outputs=simple_output)
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
with gr.Column(visible=False) as mc_interface:
|
| 244 |
+
gr.Markdown("## Multiple-Choice Template")
|
| 245 |
+
question_mc = gr.Textbox(label="Enter your question")
|
| 246 |
+
choice_A = gr.Textbox(label="Choice A")
|
| 247 |
+
choice_B = gr.Textbox(label="Choice B")
|
| 248 |
+
choice_C = gr.Textbox(label="Choice C")
|
| 249 |
+
choice_D = gr.Textbox(label="Choice D")
|
| 250 |
+
mc_output = gr.Textbox(label="Output", interactive=False)
|
| 251 |
+
submit_mc = gr.Button("Submit")
|
| 252 |
+
submit_mc.click(multiple_choice_greet, inputs=[question_mc, choice_A, choice_B, choice_C, choice_D], outputs=mc_output)
|
| 253 |
+
|
| 254 |
+
btn_t_f.click(show_true_false_interface, outputs=[true_false_interface, open_ended_interface, mc_interface])
|
| 255 |
+
btn_open_ended.click(show_open_ended_interface, outputs=[true_false_interface, open_ended_interface, mc_interface])
|
| 256 |
+
btn_m_c.click(show_multiple_choice_interface, outputs=[true_false_interface, open_ended_interface, mc_interface])
|
| 257 |
+
|
| 258 |
+
# def generate_response(instruction):
|
| 259 |
+
# """Generates a response using your fine-tuned model."""
|
| 260 |
+
# FastLanguageModel.for_inference(model) # Enable native 2x faster inference within the function
|
| 261 |
+
# prompt = f"""### Instruction:
|
| 262 |
+
# Answer the provided question with the knowledge provided to you
|
| 263 |
+
# ### Question:
|
| 264 |
+
# {instruction}
|
| 265 |
+
|
| 266 |
+
# ### Answer:
|
| 267 |
+
# """
|
| 268 |
+
|
| 269 |
+
# inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 270 |
+
# with torch.no_grad():
|
| 271 |
+
# outputs = model.generate(**inputs,early_stopping=False,min_length=50,length_penalty=2,max_length=300)
|
| 272 |
+
# response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 273 |
+
# return response
|
| 274 |
+
|
| 275 |
+
# def greet(user_question):
|
| 276 |
+
# response = generate_response(user_question)
|
| 277 |
+
# return f"{response}!"
|
| 278 |
|
| 279 |
+
# # Create a Gradio interface with text input and output
|
| 280 |
+
# gr.Interface(fn=greet, inputs="text", outputs="text", title = 'Test on Multiple Choice Questions').launch()
|
|
|
|
| 281 |
|
| 282 |
+
demo.launch(debug=True)
|
|
|