Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +32 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Load model and tokenizer
|
| 5 |
+
model_name = "valhalla/t5-small-qg-hl"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 8 |
+
|
| 9 |
+
# Function to generate question
|
| 10 |
+
def generate_question(context, answer):
|
| 11 |
+
if not context.strip() or not answer.strip():
|
| 12 |
+
return "Please enter both context and answer."
|
| 13 |
+
input_text = f"generate question: {context.replace(answer, '<hl> ' + answer + ' <hl>')}"
|
| 14 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
| 15 |
+
outputs = model.generate(inputs, max_length=64, num_beams=4, early_stopping=True)
|
| 16 |
+
question = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 17 |
+
return question
|
| 18 |
+
|
| 19 |
+
# Gradio interface
|
| 20 |
+
iface = gr.Interface(
|
| 21 |
+
fn=generate_question,
|
| 22 |
+
inputs=[
|
| 23 |
+
gr.Textbox(lines=5, label="Context or Paragraph"),
|
| 24 |
+
gr.Textbox(lines=1, label="Answer (highlighted text)")
|
| 25 |
+
],
|
| 26 |
+
outputs="text",
|
| 27 |
+
title="🧠 AI Question Generator",
|
| 28 |
+
description="Enter a paragraph and the answer you want highlighted. The app generates a relevant question."
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers==4.40.1
|
| 2 |
+
torch
|
| 3 |
+
gradio==5.34.1
|