Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### Set Up the Language Model
|
| 2 |
+
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Load a pre-trained model
|
| 6 |
+
language_model = pipeline("text-generation", model="gpt-2")
|
| 7 |
+
|
| 8 |
+
### Index with LlamaIndex
|
| 9 |
+
|
| 10 |
+
from llama_index import LlamaIndex
|
| 11 |
+
|
| 12 |
+
# Initialize LlamaIndex
|
| 13 |
+
index = LlamaIndex()
|
| 14 |
+
|
| 15 |
+
# Add documents to the index
|
| 16 |
+
documents = ["demo_data_for_RAG.pdf"]
|
| 17 |
+
index.add_documents(documents)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
### Implement RAG Logic
|
| 21 |
+
|
| 22 |
+
def retrieve_and_generate_answer(question):
|
| 23 |
+
# Retrieve relevant documents
|
| 24 |
+
retrieved_docs = index.retrieve(question)
|
| 25 |
+
|
| 26 |
+
# Generate answer using the language model
|
| 27 |
+
context = " ".join(retrieved_docs)
|
| 28 |
+
answer = language_model(context + " " + question, max_length=100)
|
| 29 |
+
return answer[0]['generated_text']
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
### Gradio Interface
|
| 33 |
+
|
| 34 |
+
import gradio as gr
|
| 35 |
+
|
| 36 |
+
def answer_question(question):
|
| 37 |
+
return retrieve_and_generate_answer(question)
|
| 38 |
+
|
| 39 |
+
# Create Gradio interface
|
| 40 |
+
iface = gr.Interface(fn=answer_question, inputs="text", outputs="text", title="Contextual QA System")
|
| 41 |
+
|
| 42 |
+
# Launch the interface
|
| 43 |
+
iface.launch()
|