Spaces:
Paused
Paused
File size: 3,934 Bytes
d1d1b25 | 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | import spaces
import torch
import gradio as gr
from transformers import (
pipeline,
BitsAndBytesConfig,
)
from duckduckgo_search import DDGS
# =====================================================
# MODEL SETUP
# =====================================================
quantization_config = (
BitsAndBytesConfig(load_in_4bit=True)
if torch.cuda.is_available()
else None
)
llama3_model_id = "meta-llama/Llama-3.1-8B-Instruct"
llama3_pipe = pipeline(
"text-generation",
model=llama3_model_id,
torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
device_map="auto",
model_kwargs={"quantization_config": quantization_config},
)
print("✅ Model Loaded")
# =====================================================
# SEARCH (HF SPACES SAFE)
# =====================================================
def google_search_results(query: str):
"""
Live web search using DuckDuckGo
(Google scraping does NOT work in Spaces)
"""
outputs = []
try:
with DDGS() as ddgs:
results = ddgs.text(query, max_results=5)
for r in results:
outputs.append(r["body"])
except Exception as e:
print("Search error:", e)
return outputs
# =====================================================
# RAG ENRICHMENT
# =====================================================
def RAG_enrichment(input_question: str):
enrichment = google_search_results(input_question)
print("Search Results:", enrichment)
new_output = (
input_question
+ "\n\nUse the following real-time information to help answer:\n\n"
)
for info in enrichment:
new_output += info + "\n\n"
return new_output
# =====================================================
# LLAMA QA
# =====================================================
@spaces.GPU
def llama_QA(input_question: str, pipe):
prompt = f"""
You are a helpful chatbot assistant.
Answer clearly and concisely.
If real-time info is missing, answer using available knowledge.
Question:
{input_question}
Answer:
"""
outputs = pipe(
prompt,
max_new_tokens=512,
do_sample=True,
temperature=0.7,
)
response = outputs[0]["generated_text"]
# remove prompt from output
response = response.replace(prompt, "").strip()
return response
# =====================================================
# GRADIO WRAPPER
# =====================================================
@spaces.GPU
def gradio_func(input_question):
print("User Question:", input_question)
# Non-RAG
output1 = llama_QA(input_question, llama3_pipe)
# RAG enriched prompt
rag_input = RAG_enrichment(input_question)
# RAG answer
output2 = llama_QA(rag_input, llama3_pipe)
return input_question, rag_input, output1, output2
# =====================================================
# UI
# =====================================================
def create_interface():
with gr.Blocks() as demo:
gr.Markdown("# 🔎 Llama3 RAG vs Non-RAG Demo")
with gr.Row():
question_input = gr.Textbox(
label="Enter your question",
value="what day is today in sydney?",
)
submit_btn = gr.Button("Ask")
with gr.Row():
input1 = gr.Textbox(label="Non-RAG Input")
input2 = gr.Textbox(label="RAG Enriched Input")
with gr.Row():
output1 = gr.Textbox(label="Non-RAG Output")
output2 = gr.Textbox(label="RAG Output")
submit_btn.click(
fn=gradio_func,
inputs=[question_input],
outputs=[input1, input2, output1, output2],
)
return demo
# =====================================================
# LAUNCH
# =====================================================
demo = create_interface()
demo.launch() |