Upload 2 files
Browse files- app.py +47 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer
|
| 6 |
+
model_name = "AventIQ-AI/gpt2-book-article-recommendation"
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name).to(device)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
def recommend_titles(alphabet, num_recommendations=5):
|
| 12 |
+
"""Generate book/article recommendations based on an input alphabet."""
|
| 13 |
+
input_text = alphabet.strip()
|
| 14 |
+
if not input_text:
|
| 15 |
+
return ["β οΈ Please enter a valid letter."]
|
| 16 |
+
|
| 17 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
|
| 18 |
+
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
outputs = model.generate(input_ids, max_length=15, num_return_sequences=num_recommendations, do_sample=True)
|
| 21 |
+
|
| 22 |
+
return [tokenizer.decode(output, skip_special_tokens=True) for output in outputs]
|
| 23 |
+
|
| 24 |
+
# Example Inputs
|
| 25 |
+
example_inputs = ["A", "B", "C", "D", "E"]
|
| 26 |
+
|
| 27 |
+
# Create Gradio Interface
|
| 28 |
+
with gr.Blocks() as demo:
|
| 29 |
+
gr.Markdown("## π AI-Powered Book & Article Recommendation")
|
| 30 |
+
gr.Markdown("Enter a letter, and the AI will suggest relevant book or article titles!")
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
alphabet_input = gr.Textbox(label="π Enter a Letter:", placeholder="Example: A")
|
| 34 |
+
num_recommendations = gr.Slider(minimum=1, maximum=10, value=5, step=1, label="Number of Recommendations")
|
| 35 |
+
|
| 36 |
+
recommend_button = gr.Button("π Get Recommendations")
|
| 37 |
+
output_text = gr.Textbox(label="π Recommended Titles:", lines=6)
|
| 38 |
+
|
| 39 |
+
gr.Markdown("### π― Example Inputs")
|
| 40 |
+
example_buttons = [gr.Button(example) for example in example_inputs]
|
| 41 |
+
|
| 42 |
+
for btn in example_buttons:
|
| 43 |
+
btn.click(fn=lambda letter=btn.value: letter, outputs=alphabet_input)
|
| 44 |
+
|
| 45 |
+
recommend_button.click(recommend_titles, inputs=[alphabet_input, num_recommendations], outputs=output_text)
|
| 46 |
+
|
| 47 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|
| 4 |
+
sentencepiece
|
| 5 |
+
torchvision
|
| 6 |
+
huggingface_hub
|
| 7 |
+
pillow
|
| 8 |
+
numpy
|