add initial project setup with Gradio chat interface and model integration
Browse files- .idea/.gitignore +8 -0
- .python-version +1 -0
- app.py +87 -0
- pyproject.toml +9 -0
- uv.lock +0 -0
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.13
|
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Configuration
|
| 7 |
+
MODEL_REPO = "SaiBon99/llama-finetuned-gguf"
|
| 8 |
+
MODEL_FILE = "model.gguf"
|
| 9 |
+
|
| 10 |
+
print(f"Downloading model from {MODEL_REPO}...")
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
# Download the GGUF model file from HuggingFace
|
| 14 |
+
model_path = hf_hub_download(
|
| 15 |
+
repo_id=MODEL_REPO,
|
| 16 |
+
filename=MODEL_FILE,
|
| 17 |
+
repo_type="model"
|
| 18 |
+
)
|
| 19 |
+
print(f"Model downloaded to: {model_path}")
|
| 20 |
+
|
| 21 |
+
# Load the model with llama-cpp-python
|
| 22 |
+
print("Loading model into memory...")
|
| 23 |
+
llm = Llama(
|
| 24 |
+
model_path=model_path,
|
| 25 |
+
n_ctx=2048, # Context window
|
| 26 |
+
n_threads=4, # Number of CPU threads
|
| 27 |
+
n_gpu_layers=0, # Set to 0 for CPU, increase for GPU
|
| 28 |
+
verbose=False,
|
| 29 |
+
)
|
| 30 |
+
print("Model loaded successfully!")
|
| 31 |
+
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error loading model: {e}")
|
| 34 |
+
llm = None
|
| 35 |
+
|
| 36 |
+
def chat(message, history):
|
| 37 |
+
"""
|
| 38 |
+
Chat function that takes a message and chat history,
|
| 39 |
+
and returns the model's response.
|
| 40 |
+
"""
|
| 41 |
+
if llm is None:
|
| 42 |
+
return "Error: Model failed to load. Please check the logs."
|
| 43 |
+
|
| 44 |
+
# Build the conversation in Llama 3 chat format
|
| 45 |
+
conversation = "<|begin_of_text|>"
|
| 46 |
+
|
| 47 |
+
if history:
|
| 48 |
+
for user_msg, bot_msg in history:
|
| 49 |
+
conversation += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|>"
|
| 50 |
+
conversation += f"<|start_header_id|>assistant<|end_header_id|>\n\n{bot_msg}<|eot_id|>"
|
| 51 |
+
|
| 52 |
+
# Add the current message
|
| 53 |
+
conversation += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|>"
|
| 54 |
+
conversation += "<|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 55 |
+
|
| 56 |
+
# Generate response
|
| 57 |
+
try:
|
| 58 |
+
response = llm(
|
| 59 |
+
conversation,
|
| 60 |
+
max_tokens=512,
|
| 61 |
+
temperature=0.7,
|
| 62 |
+
top_p=0.9,
|
| 63 |
+
repeat_penalty=1.1,
|
| 64 |
+
stop=["<|eot_id|>", "<|start_header_id|>"],
|
| 65 |
+
echo=False
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
return response['choices'][0]['text'].strip()
|
| 69 |
+
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return f"Error generating response: {str(e)}"
|
| 72 |
+
|
| 73 |
+
# Create Gradio ChatInterface
|
| 74 |
+
demo = gr.ChatInterface(
|
| 75 |
+
fn=chat,
|
| 76 |
+
title="Llama 3.2 3B Fine-tuned Chat",
|
| 77 |
+
description=f"Chat with Llama 3.2 3B fine-tuned on FineTome-100k dataset (GGUF format)\n\nModel: `{MODEL_REPO}`",
|
| 78 |
+
examples=[
|
| 79 |
+
"Explain what boolean operators are and how they work in programming.",
|
| 80 |
+
"What is the difference between short-circuit evaluation and normal evaluation?",
|
| 81 |
+
"Write a Python function to check if a number is prime.",
|
| 82 |
+
"Explain the concept of operator precedence with examples.",
|
| 83 |
+
],
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
pyproject.toml
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "iris"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.13"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"gradio>=6.0.1",
|
| 9 |
+
]
|
uv.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|