|
import gradio as gr |
|
from transformers import LlamaTokenizer, LlamaForCausalLM |
|
import torch |
|
import os |
|
from huggingface_hub import login |
|
|
|
|
|
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.") |
|
|
|
|
|
def predict(input_text): |
|
if not input_text: |
|
return "Please enter some text to analyze." |
|
|
|
try: |
|
|
|
tokenizer = LlamaTokenizer.from_pretrained("./fine_tuned_llama2") |
|
model = LlamaForCausalLM.from_pretrained("./fine_tuned_llama2") |
|
model.eval() |
|
|
|
|
|
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, padding="max_length", truncation=True) |
|
|
|
with torch.no_grad(): |
|
outputs = model.generate(**inputs, max_new_tokens=50) |
|
|
|
result = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
return result |
|
except Exception as e: |
|
return f"Error during prediction: {e}" |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
interface.launch() |