Spaces:
Sleeping
Sleeping
Initial update for our case
Browse files
app.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
# chatbot_template.py
|
| 2 |
-
|
| 3 |
-
import gradio as gr
|
| 4 |
import spaces
|
| 5 |
|
| 6 |
DESCRIPTION = """
|
|
@@ -16,50 +14,64 @@ LICENSE = """
|
|
| 16 |
|
| 17 |
# This is a dummy generation function
|
| 18 |
@spaces.GPU # This allows it to run on GPU Spaces (remove if not needed)
|
| 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 |
if __name__ == "__main__":
|
| 65 |
demo.queue().launch()
|
|
|
|
| 1 |
# chatbot_template.py
|
|
|
|
|
|
|
| 2 |
import spaces
|
| 3 |
|
| 4 |
DESCRIPTION = """
|
|
|
|
| 14 |
|
| 15 |
# This is a dummy generation function
|
| 16 |
@spaces.GPU # This allows it to run on GPU Spaces (remove if not needed)
|
| 17 |
+
import torch
|
| 18 |
+
import gradio as gr
|
| 19 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 20 |
+
from peft import PeftModel
|
| 21 |
+
|
| 22 |
+
# === [1] Model and Tokenizer Loading ===
|
| 23 |
+
base_model_id = "meta-llama/Llama-2-7b-hf" # Replace with your base model
|
| 24 |
+
lora_path = "./tat-llm" # Path to your fine-tuned LoRA folder
|
| 25 |
+
|
| 26 |
+
# Load base model and LoRA adapter
|
| 27 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_model_id, torch_dtype=torch.float16)
|
| 28 |
+
model = PeftModel.from_pretrained(base_model, lora_path)
|
| 29 |
+
model.eval().cuda()
|
| 30 |
+
|
| 31 |
+
# Load tokenizer
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(lora_path)
|
| 33 |
+
|
| 34 |
+
# === [2] Prompt Formatting Function ===
|
| 35 |
+
def create_prompt(table, context, question):
|
| 36 |
+
return f"""You are a financial assistant. Given the table and context, answer the question.
|
| 37 |
+
|
| 38 |
+
Table:
|
| 39 |
+
{table}
|
| 40 |
+
|
| 41 |
+
Context:
|
| 42 |
+
{context}
|
| 43 |
+
|
| 44 |
+
Question:
|
| 45 |
+
{question}
|
| 46 |
+
|
| 47 |
+
Answer:"""
|
| 48 |
+
|
| 49 |
+
# === [3] Inference Function ===
|
| 50 |
+
def answer_question(table, context, question):
|
| 51 |
+
prompt = create_prompt(table, context, question)
|
| 52 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 53 |
+
with torch.no_grad():
|
| 54 |
+
outputs = model.generate(
|
| 55 |
+
**inputs,
|
| 56 |
+
max_new_tokens=128,
|
| 57 |
+
do_sample=False,
|
| 58 |
+
eos_token_id=tokenizer.eos_token_id
|
| 59 |
+
)
|
| 60 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
| 61 |
+
|
| 62 |
+
# === [4] Gradio UI Layout ===
|
| 63 |
+
with gr.Blocks(title="TAT-LLM Table & Text QA") as demo:
|
| 64 |
+
gr.Markdown("## TAT-LLM: Table-and-Text Question Answering\nUpload a table (Markdown format), provide context, and ask your question.")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
table_input = gr.Textbox(label="Table (Markdown)", lines=10, placeholder="| Quarter | Revenue |\n|--------|---------|\n| Q1 | 100 | ...")
|
| 68 |
+
context_input = gr.Textbox(label="Context", lines=10, placeholder="PT ABC mengalami peningkatan pendapatan dari Q1 ke Q4.")
|
| 69 |
+
question_input = gr.Textbox(label="Question", lines=2, placeholder="Berapa persentase kenaikan dari Q1 ke Q4?")
|
| 70 |
+
output_box = gr.Textbox(label="Answer", lines=5)
|
| 71 |
+
|
| 72 |
+
submit_btn = gr.Button("Generate Answer")
|
| 73 |
+
submit_btn.click(fn=answer_question, inputs=[table_input, context_input, question_input], outputs=output_box)
|
| 74 |
|
| 75 |
+
# === [5] Launch ===
|
| 76 |
if __name__ == "__main__":
|
| 77 |
demo.queue().launch()
|