Spaces:
Sleeping
Sleeping
File size: 2,001 Bytes
c4e9be0 1a05580 5850382 d3151ec c4e9be0 1a05580 f7ec7b4 78fe0f4 d98c763 1a05580 d98c763 bd9a4bb d3151ec d98c763 1a05580 d98c763 1a05580 78fe0f4 1a05580 78fe0f4 1a05580 3b0aae0 78fe0f4 1a05580 |
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 |
import gradio as gr
from concurrent.futures import ThreadPoolExecutor
import fine_tuning
import rag
# -----------------------------
# Load fine-tuned model
# -----------------------------
fine_tuning.load_and_train()
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
def combined_generate(prompt, max_tokens):
with ThreadPoolExecutor() as executor:
start_times = {"Fine-tuned": time.time(), "RAG": time.time()}
futures = {
executor.submit(fine_tuning.generate_answer, prompt, max_tokens): "Fine-tuned",
executor.submit(rag.generate_answer, prompt): "RAG",
}
answers = {"Fine-tuned": "", "RAG": ""}
times = {"Fine-tuned": None, "RAG": None}
for future in as_completed(futures):
model_name = futures[future]
answers[model_name] = future.result()
times[model_name] = round(time.time() - start_times[model_name], 2)
# Format answers with time taken
ft_display = answers["Fine-tuned"]
if times["Fine-tuned"] is not None:
ft_display += f"\n\n⏱ Took {times['Fine-tuned']}s"
rag_display = answers["RAG"]
if times["RAG"] is not None:
rag_display += f"\n\n⏱ Took {times['RAG']}s"
yield ft_display, rag_display
# -----------------------------
# Gradio Interface
# -----------------------------
iface = gr.Interface(
fn=combined_generate,
inputs=[
gr.Textbox(label="Enter your question:", lines=5, placeholder="Type your question here..."),
gr.Slider(minimum=50, maximum=500, step=10, value=200, label="Max tokens to generate")
],
outputs=[
gr.Textbox(label="Fine-tuned Model Answer"),
gr.Textbox(label="RAG Answer")
],
title="Compare Fine-tuned Model vs RAG 🤖📚",
description="Ask a question and get answers from both the fine-tuned model and the RAG pipeline."
).queue()
iface.launch()
|