|
import gradio as gr |
|
import spaces |
|
import torch |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
|
|
from services.rag_pipeline import rag_pipeline |
|
|
|
model_name = "dasomaru/gemma-3-4bit-it-demo" |
|
|
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) |
|
|
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_name, |
|
torch_dtype=torch.float16, |
|
device_map="auto", |
|
trust_remote_code=True, |
|
) |
|
|
|
|
|
search_cache = {} |
|
|
|
@spaces.GPU(duration=300) |
|
def generate_response(query: str): |
|
tokenizer = AutoTokenizer.from_pretrained( |
|
"dasomaru/gemma-3-4bit-it-demo", |
|
trust_remote_code=True, |
|
) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
"dasomaru/gemma-3-4bit-it-demo", |
|
torch_dtype=torch.float16, |
|
device_map="auto", |
|
trust_remote_code=True, |
|
|
|
) |
|
model.to("cuda") |
|
|
|
if query in search_cache: |
|
print(f"โก ์บ์ ์ฌ์ฉ: '{query}'") |
|
return search_cache[query] |
|
|
|
|
|
|
|
top_k = 5 |
|
results = rag_pipeline(query, top_k=top_k) |
|
|
|
|
|
if isinstance(results, list): |
|
results = "\n\n".join(results) |
|
|
|
search_cache[query] = results |
|
|
|
|
|
inputs = tokenizer(results, return_tensors="pt").to(model.device) |
|
outputs = model.generate( |
|
**inputs, |
|
max_new_tokens=512, |
|
temperature=0.7, |
|
top_p=0.9, |
|
top_k=50, |
|
do_sample=True, |
|
) |
|
|
|
return tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
|
|
demo = gr.Interface( |
|
fn=generate_response, |
|
|
|
inputs="text", |
|
outputs="text", |
|
title="Law RAG Assistant", |
|
description="๋ฒ๋ น ๊ธฐ๋ฐ RAG ํ์ดํ๋ผ์ธ ํ
์คํธ", |
|
) |
|
|
|
|
|
|
|
demo.launch(debug=True) |
|
|