Spaces:
Sleeping
Sleeping
File size: 1,322 Bytes
407a115 ea6a575 407a115 ea6a575 afe0d17 25387ea ea6a575 ff86d76 407a115 ea6a575 afe0d17 ea6a575 25387ea ea6a575 25387ea ea6a575 d3072cc 25387ea d3072cc 25387ea afe0d17 25387ea d3072cc ea6a575 |
1 2 3 4 5 6 7 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 |
import gradio as gr
from transformers import pipeline
import re
# Load model
generator = pipeline(
"text-generation",
model="tiiuae/falcon-7b-instruct",
use_auth_token="HF_TOKEN2" # Replace with your real token
)
def parse_flashcards(text):
# Extract Q/A pairs using regex
qa_pairs = re.findall(r"Q[:\-]?\s*(.*?)\s*A[:\-]?\s*(.*?)(?=Q[:\-]|$)", text, re.DOTALL)
flashcards = []
for i, (q, a) in enumerate(qa_pairs, start=1):
flashcards.append(f"Q{i}: {q.strip()}\nA{i}: {a.strip()}")
return "\n\n".join(flashcards) if flashcards else text
def generate_flashcards(topic):
prompt = f"Create 5 unique flashcards about '{topic}'. Format:\nQ: <question>\nA: <answer>\n"
response = generator(
prompt,
max_new_tokens=300,
temperature=0.7,
top_p=0.9
)
raw_text = response[0]["generated_text"]
return parse_flashcards(raw_text)
with gr.Blocks() as demo:
gr.Markdown("## π AI Flashcard Generator\nEnter a topic to generate flashcards instantly.")
with gr.Row():
topic = gr.Textbox(label="Enter Topic", placeholder="e.g. Python basics")
btn = gr.Button("Generate Flashcards")
output = gr.Textbox(label="Flashcards", lines=15)
btn.click(generate_flashcards, inputs=topic, outputs=output)
demo.launch() |