File size: 1,986 Bytes
5d6c18f |
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 40 41 42 43 44 45 46 47 48 |
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
# Load the model and tokenizer
model_name = "AventIQ-AI/gpt2-book-article-recommendation"
device = "cuda" if torch.cuda.is_available() else "cpu"
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
tokenizer = AutoTokenizer.from_pretrained(model_name)
def recommend_titles(alphabet, num_recommendations=5):
"""Generate book/article recommendations based on an input alphabet."""
input_text = alphabet.strip()
if not input_text:
return ["β οΈ Please enter a valid letter."]
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
with torch.no_grad():
outputs = model.generate(input_ids, max_length=15, num_return_sequences=num_recommendations, do_sample=True)
return [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
# Example Inputs
example_inputs = ["A", "B", "C", "D", "E"]
# Create Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("## π AI-Powered Book & Article Recommendation")
gr.Markdown("Enter a letter, and the AI will suggest relevant book or article titles!")
with gr.Row():
alphabet_input = gr.Textbox(label="π Enter a Letter:", placeholder="Example: A")
num_recommendations = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Number of Recommendations")
recommend_button = gr.Button("π Get Recommendations")
output_text = gr.Textbox(label="π Recommended Titles:", lines=6)
gr.Markdown("### π― Example Inputs")
example_buttons = [gr.Button(example) for example in example_inputs]
for btn in example_buttons:
btn.click(fn=lambda letter=btn.value: letter, outputs=alphabet_input)
recommend_button.click(recommend_titles, inputs=[alphabet_input, num_recommendations], outputs=output_text)
demo.launch()
|