Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,64 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
)
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
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 |
-
|
|
|
1 |
+
import json
|
2 |
+
from transformers import pipeline
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
+
# Load question-generation and question-answering pipelines
|
6 |
+
qg_pipeline = pipeline("e2e-qg", model="valhalla/t5-small-qa-qg-hl")
|
7 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad")
|
|
|
8 |
|
9 |
+
# Simple chunking: split on paragraphs (for demo)
|
10 |
+
def split_chunks(text, max_len=200):
|
11 |
+
paragraphs = [p.strip() for p in text.split("\n") if p.strip()]
|
12 |
+
chunks = []
|
13 |
+
for p in paragraphs:
|
14 |
+
# further split long paragraphs
|
15 |
+
words = p.split()
|
16 |
+
if len(words) <= max_len:
|
17 |
+
chunks.append(p)
|
18 |
+
else:
|
19 |
+
for i in range(0, len(words), max_len):
|
20 |
+
chunk = " ".join(words[i : i + max_len])
|
21 |
+
chunks.append(chunk)
|
22 |
+
return chunks
|
23 |
|
24 |
+
# Conversion function
|
25 |
+
def convert_text(raw_text):
|
26 |
+
chunks = split_chunks(raw_text)
|
27 |
+
qna_list = []
|
28 |
+
for chunk in chunks:
|
29 |
+
# Generate raw Q&A pairs
|
30 |
+
try:
|
31 |
+
candidates = qg_pipeline(chunk)
|
32 |
+
except Exception:
|
33 |
+
continue
|
34 |
+
for cand in candidates:
|
35 |
+
question = cand.get("question") or cand.get("Q")
|
36 |
+
if not question:
|
37 |
+
continue
|
38 |
+
# Refine answer using QA pipeline
|
39 |
+
ans = qa_pipeline({"question": question, "context": chunk})
|
40 |
+
answer = ans.get("answer", "").strip()
|
41 |
+
# Append result
|
42 |
+
qna_list.append({"question": question.strip(), "answer": answer})
|
43 |
+
# Deduplicate
|
44 |
+
unique = []
|
45 |
+
seen = set()
|
46 |
+
for qa in qna_list:
|
47 |
+
key = (qa['question'], qa['answer'])
|
48 |
+
if key not in seen:
|
49 |
+
unique.append(qa)
|
50 |
+
seen.add(key)
|
51 |
+
return json.dumps(unique, indent=2, ensure_ascii=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# Gradio interface
|
54 |
+
def main():
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("# Handbook Text to Q&A Converter")
|
57 |
+
input_text = gr.Textbox(lines=10, placeholder="Paste handbook text here...", label="Raw Text")
|
58 |
+
output_json = gr.Textbox(lines=10, label="Generated Q&A JSON")
|
59 |
+
convert_btn = gr.Button("Convert")
|
60 |
+
convert_btn.click(fn=convert_text, inputs=input_text, outputs=output_json)
|
61 |
+
demo.launch()
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
+
main()
|