PhunvVi commited on
Commit
fcc3183
·
verified ·
1 Parent(s): 3ff4680

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -48
app.py CHANGED
@@ -6,59 +6,62 @@ For more information on `huggingface_hub` Inference API support, please check th
6
  """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
 
 
 
 
 
 
 
 
 
 
25
 
26
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
27
 
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
6
  """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
9
+ from model import load_model, load_tokenizer
10
+ from utils import clean_output, get_shap_values
11
+ import torch
12
+ import shap
13
+ import matplotlib.pyplot as plt
14
 
15
+ tokenizer = load_tokenizer()
16
+ model = load_model()
 
 
 
 
 
 
 
17
 
18
+ def gradio_generate(context, num_questions, max_length):
19
+ input_prompt = f"generate question: {context.strip()}"
20
+ inputs = tokenizer(input_prompt, return_tensors="pt", truncation=True, padding="longest").to(model.device)
21
+ outputs = model.generate(
22
+ input_ids=inputs["input_ids"],
23
+ attention_mask=inputs["attention_mask"],
24
+ max_length=max_length,
25
+ num_return_sequences=num_questions,
26
+ do_sample=True,
27
+ top_p=0.95,
28
+ temperature=1.0
29
+ )
30
+ decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)
31
+ questions = clean_output(decoded)
32
+ return "\n".join([f"{i+1}. {q}" for i, q in enumerate(questions)])
33
 
34
+ def gradio_shap(context):
35
+ input_prompt = f"generate question: {context.strip()}"
36
+ try:
37
+ shap_values, tokens = get_shap_values(tokenizer, model, input_prompt)
38
+ fig, ax = plt.subplots(figsize=(10, 2))
39
+ shap.plots.text(shap.Explanation(values=shap_values, data=tokens), display=False)
40
+ plt.tight_layout()
41
+ return fig
42
+ except Exception as e:
43
+ return f"SHAP explanation failed: {e}"
44
 
45
+ with gr.Blocks() as demo:
46
+ gr.Markdown("# 🧠 C3QG – Context-Controlled Question Generation with FLAN-T5")
47
+ context = gr.Textbox(label="📄 Paste your context paragraph here:", lines=6)
48
+ num_questions = gr.Slider(1, 5, value=3, label="Number of Questions")
49
+ max_length = gr.Slider(32, 128, value=64, label="Max Output Tokens")
50
+ generate_btn = gr.Button("🔄 Generate Questions")
51
+ questions_output = gr.Textbox(label="🎯 Generated Questions")
52
+ shap_btn = gr.Button("Show SHAP Explanation")
53
+ shap_output = gr.Plot(label="SHAP Token Importance")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ generate_btn.click(
56
+ gradio_generate,
57
+ inputs=[context, num_questions, max_length],
58
+ outputs=questions_output
59
+ )
60
+ shap_btn.click(
61
+ gradio_shap,
62
+ inputs=context,
63
+ outputs=shap_output
64
+ )
65
 
66
  if __name__ == "__main__":
67
  demo.launch()