Spaces:
Runtime error
Runtime error
File size: 1,505 Bytes
2ea93fe 3453878 ef9cf57 2ea93fe 22707c7 2ea93fe ef9cf57 2ea93fe 22707c7 2ea93fe ef9cf57 2ea93fe |
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 |
import gradio as gr
import pandas as pd
import spaces
from transformers import RagTokenizer, RagRetriever, RagTokenForGeneration
# Load the tokenizer and retriever
tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", use_dummy_dataset=True)
# Load the model
model = RagTokenForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)
# Tokenize the contexts and responses
inputs = tokenizer(contexts, return_tensors='pt', padding=True, truncation=True)
labels = tokenizer(responses, return_tensors='pt', padding=True, truncation=True)
# Extract the abstracts
abstracts = df['Abstract'].dropna().tolist()
# Load your dataset
df = pd.read_csv('10kstats.csv')
# Generate context-response pairs (abstract-question pairs)
# Here we use the abstracts as contexts and simulate questions
contexts = abstracts
responses = ["Can you tell me more about this research?" for _ in abstracts]
@spaces.GPU
def generate_response(input_text):
input_ids = tokenizer([input_text], return_tensors='pt')['input_ids']
outputs = model.generate(input_ids)
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
return response
# Create the Gradio interface
iface = gr.Interface(
fn=generate_response,
inputs="text",
outputs="text",
title="RAG Chatbot",
description="A chatbot powered by Retrieval-Augmented Generation (RAG) model."
)
# Launch the interface
iface.launch() |