File size: 1,852 Bytes
bc32b76 4b6c42c bc32b76 5b0960f b4ff959 4b6c42c 112890f b4ff959 5b0960f 112890f 4b6c42c e6fa3be 4b6c42c e6fa3be 4b6c42c |
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 |
import gradio as gr
from transformers import LlamaTokenizer, LlamaForCausalLM
import torch
import os
from huggingface_hub import login
# Authenticate with Hugging Face using the token from Space secrets
hf_token = os.getenv("HF_TOKEN")
if hf_token:
login(token=hf_token)
print("Authenticated with Hugging Face token.")
else:
print("HF_TOKEN not found in environment variables. Please set it in Space secrets.")
# Function to predict fraud based on text input
def predict(input_text):
if not input_text:
return "Please enter some text to analyze."
try:
# Load the fine-tuned model and tokenizer from the local directory
tokenizer = LlamaTokenizer.from_pretrained("./fine_tuned_llama2")
model = LlamaForCausalLM.from_pretrained("./fine_tuned_llama2")
model.eval()
# Tokenize input
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, padding="max_length", truncation=True)
# Generate output
with torch.no_grad():
outputs = model.generate(**inputs, max_new_tokens=50)
# Decode and return result
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
return result
except Exception as e:
return f"Error during prediction: {e}"
# Create Gradio interface with text input
interface = gr.Interface(
fn=predict,
inputs=gr.Textbox(
lines=2,
placeholder="Enter text to analyze (e.g., 'Facility backdates policies. Is this fraudulent?')",
label="Input Text"
),
outputs=gr.Textbox(label="Prediction"),
title="Fine-Tune LLaMA 2 for Healthcare Fraud Analysis",
description="Test the fine-tuned LLaMA 2 model to detect healthcare fraud. Enter a description of a facility's behavior to analyze."
)
# Launch the interface
interface.launch() |