S-Dreamer commited on
Commit
6568226
·
verified ·
1 Parent(s): c324f97

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForQuestionAnswering, AutoTokenizer
3
+ import torch
4
+
5
+ # Load model and tokenizer
6
+ MODEL_NAME = "your-hf-username/raft-qa"
7
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
8
+ model = AutoModelForQuestionAnswering.from_pretrained(MODEL_NAME)
9
+
10
+ def answer_question(context, question):
11
+ inputs = tokenizer(question, context, return_tensors="pt", truncation=True, max_length=512)
12
+ with torch.no_grad():
13
+ outputs = model(**inputs)
14
+
15
+ start_scores, end_scores = outputs.start_logits, outputs.end_logits
16
+ start_idx = torch.argmax(start_scores)
17
+ end_idx = torch.argmax(end_scores) + 1
18
+ answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs["input_ids"][0][start_idx:end_idx]))
19
+
20
+ return answer if answer.strip() else "No answer found."
21
+
22
+ # Define UI
23
+ with gr.Blocks(theme="soft") as demo:
24
+ gr.Markdown("# 🤖 RAFT: Retrieval-Augmented Fine-Tuning for QA")
25
+ gr.Markdown("Ask a question based on the provided context and see how RAFT improves response accuracy!")
26
+
27
+ with gr.Row():
28
+ context_input = gr.Textbox(lines=5, label="Context", placeholder="Enter background text here...")
29
+ question_input = gr.Textbox(lines=2, label="Question", placeholder="What is the main idea?")
30
+
31
+ answer_output = gr.Textbox(label="Answer", interactive=False)
32
+
33
+ submit_btn = gr.Button("Generate Answer")
34
+ submit_btn.click(answer_question, inputs=[context_input, question_input], outputs=answer_output)
35
+
36
+ demo.launch()