File size: 1,035 Bytes
2304c51
 
 
 
 
f72ff93
2304c51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
### Set Up the Language Model

from transformers import pipeline

# Load a pre-trained model
language_model = pipeline("text-generation", model="gpt2", clean_up_tokenization_spaces=True)

### Index with LlamaIndex

from llama_index import LlamaIndex

# Initialize LlamaIndex
index = LlamaIndex()

# Add documents to the index
documents = ["demo_data_for_RAG.pdf"]
index.add_documents(documents)


### Implement RAG Logic

def retrieve_and_generate_answer(question):
    # Retrieve relevant documents
    retrieved_docs = index.retrieve(question)

    # Generate answer using the language model
    context = " ".join(retrieved_docs)
    answer = language_model(context + " " + question, max_length=100)
    return answer[0]['generated_text']


### Gradio Interface

import gradio as gr

def answer_question(question):
    return retrieve_and_generate_answer(question)

# Create Gradio interface
iface = gr.Interface(fn=answer_question, inputs="text", outputs="text", title="Contextual QA System")

# Launch the interface
iface.launch()