Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,64 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
# Set the device for model inference
|
| 7 |
-
# This will automatically use the GPU if one is available and configured
|
| 8 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
|
| 10 |
-
# Load the tokenizer
|
| 11 |
-
tokenizer = AutoTokenizer.from_pretrained("google/medgemma-4b-it")
|
| 12 |
-
|
| 13 |
-
# Load the model and ensure it's on the correct device
|
| 14 |
-
# We've added `load_in_8bit=True` to reduce the memory footprint.
|
| 15 |
-
# We've also added `offload_folder` to explicitly enable disk offloading
|
| 16 |
-
# for the model when it can't fit into VRAM or system RAM.
|
| 17 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
-
"google/medgemma-4b-it",
|
| 19 |
-
torch_dtype=torch.bfloat16,
|
| 20 |
-
device_map="auto",
|
| 21 |
-
load_in_8bit=True,
|
| 22 |
-
offload_folder="./offload_dir"
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
def chat_interface(message, history):
|
| 26 |
-
"""
|
| 27 |
-
Main chat function to interact with the model.
|
| 28 |
-
"""
|
| 29 |
-
# The Gradio history is a list of lists.
|
| 30 |
-
# We need to format the chat history for the tokenizer's chat template.
|
| 31 |
-
chat_history = []
|
| 32 |
-
for user_msg, bot_msg in history:
|
| 33 |
-
chat_history.append({"role": "user", "content": user_msg})
|
| 34 |
-
chat_history.append({"role": "assistant", "content": bot_msg})
|
| 35 |
-
|
| 36 |
-
# Add the current user message to the chat history
|
| 37 |
-
chat_history.append({"role": "user", "content": message})
|
| 38 |
-
|
| 39 |
-
# Apply the tokenizer's chat template
|
| 40 |
-
prompt = tokenizer.apply_chat_template(chat_history, tokenize=False, add_generation_prompt=True)
|
| 41 |
-
|
| 42 |
-
# Generate the response
|
| 43 |
-
input_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
|
| 44 |
-
outputs = model.generate(
|
| 45 |
-
input_ids.to(device),
|
| 46 |
-
max_new_tokens=256,
|
| 47 |
-
do_sample=True,
|
| 48 |
-
temperature=0.7,
|
| 49 |
-
top_k=50,
|
| 50 |
-
top_p=0.95
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
# Decode the response and extract the model's part
|
| 54 |
-
response = tokenizer.decode(outputs[0])
|
| 55 |
-
response = response.split("<end_of_turn>")[1].strip()
|
| 56 |
-
|
| 57 |
-
return response
|
| 58 |
-
|
| 59 |
-
# Create the Gradio interface
|
| 60 |
-
gr.ChatInterface(
|
| 61 |
-
fn=chat_interface,
|
| 62 |
-
title="MedGemma-4B-IT Medical Assistant",
|
| 63 |
-
description="A fine-tuned model for medical-related questions."
|
| 64 |
-
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|