Spaces:
Runtime error
Runtime error
Add Gradio chatbot for fine-tuned Zephyr model new space
Browse files- app.py +61 -0
- requirments.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 4 |
+
from peft import PeftModel
|
| 5 |
+
|
| 6 |
+
# Model setup
|
| 7 |
+
base_model = "HuggingFaceH4/zephyr-7b-beta"
|
| 8 |
+
model_path = r"hmorad/zephyr-geo-finetuned/tree/main/checkpoint-161" # Update to your repo ID
|
| 9 |
+
|
| 10 |
+
# Quantization config
|
| 11 |
+
bnb_config = BitsAndBytesConfig(
|
| 12 |
+
load_in_4bit=True,
|
| 13 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 14 |
+
bnb_4bit_use_double_quant=True,
|
| 15 |
+
bnb_4bit_quant_type="nf4"
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Load tokenizer
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model)
|
| 20 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 21 |
+
|
| 22 |
+
# Load base model
|
| 23 |
+
base = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
base_model,
|
| 25 |
+
quantization_config=bnb_config,
|
| 26 |
+
device_map="auto",
|
| 27 |
+
trust_remote_code=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Load fine-tuned LoRA adapter
|
| 31 |
+
model = PeftModel.from_pretrained(base, model_path)
|
| 32 |
+
model.eval()
|
| 33 |
+
|
| 34 |
+
def generate_response(prompt, max_new_tokens=200):
|
| 35 |
+
input_text = f"user: {prompt}\nassistant:"
|
| 36 |
+
inputs = tokenizer(input_text, return_tensors="pt").to("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
output = model.generate(
|
| 39 |
+
**inputs,
|
| 40 |
+
max_new_tokens=max_new_tokens,
|
| 41 |
+
temperature=0.6,
|
| 42 |
+
do_sample=True,
|
| 43 |
+
top_k=50,
|
| 44 |
+
top_p=0.9,
|
| 45 |
+
pad_token_id=tokenizer.eos_token_id
|
| 46 |
+
)
|
| 47 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 48 |
+
return decoded.split("assistant:")[-1].strip()
|
| 49 |
+
|
| 50 |
+
# Gradio interface
|
| 51 |
+
iface = gr.Interface(
|
| 52 |
+
fn=generate_response,
|
| 53 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter your geospatial query (e.g., 'Convert this shapefile to PostGIS')"),
|
| 54 |
+
outputs="text",
|
| 55 |
+
title="Geospatial Data Chatbot",
|
| 56 |
+
description="Ask questions about geospatial data processing.",
|
| 57 |
+
theme="huggingface"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
iface.launch()
|
requirments.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
transformers==4.52.4
|
| 3 |
+
peft==0.15.2
|
| 4 |
+
bitsandbytes==0.44.0
|
| 5 |
+
gradio==4.44.0
|
| 6 |
+
huggingface_hub>=0.30.0
|