update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,80 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
),
|
| 60 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
-
with gr.Blocks() as demo:
|
| 64 |
-
with gr.Sidebar():
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
-
|
| 68 |
-
|
| 69 |
if __name__ == "__main__":
|
| 70 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
|
| 4 |
+
# Load YOUR fine-tuned model
|
| 5 |
+
model_path = "samzito12/finetome-llama-3b-gguf"
|
| 6 |
|
| 7 |
+
print("Loading model...")
|
| 8 |
+
llm = Llama.from_pretrained(
|
| 9 |
+
repo_id=model_path,
|
| 10 |
+
filename="*Q4_K_M.gguf",
|
| 11 |
+
n_ctx=2048,
|
| 12 |
+
n_threads=2,
|
| 13 |
+
verbose=False
|
| 14 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# System prompt to fix identity issue
|
| 17 |
+
SYSTEM_PROMPT = """You are a helpful AI assistant based on Meta's Llama-3.2-3B model, fine-tuned on the FineTome dataset. You are NOT ChatGPT and you are NOT made by OpenAI. You were created as part of a university machine learning project."""
|
| 18 |
|
| 19 |
+
def chat(message, history):
|
| 20 |
+
"""Generate response from YOUR fine-tuned model"""
|
| 21 |
+
|
| 22 |
+
# Build conversation with system prompt
|
| 23 |
+
conversation = f"<|start_header_id|>system<|end_header_id|>\n\n{SYSTEM_PROMPT}<|eot_id|>"
|
| 24 |
+
|
| 25 |
+
# Add chat history
|
| 26 |
+
for user_msg, assistant_msg in history:
|
| 27 |
+
conversation += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|>"
|
| 28 |
+
conversation += f"<|start_header_id|>assistant<|end_header_id|>\n\n{assistant_msg}<|eot_id|>"
|
| 29 |
+
|
| 30 |
+
# Add current message
|
| 31 |
+
conversation += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 32 |
+
|
| 33 |
+
# Generate response
|
| 34 |
+
response = llm(
|
| 35 |
+
conversation,
|
| 36 |
+
max_tokens=512,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
top_p=0.9,
|
| 39 |
+
stop=["<|eot_id|>", "<|start_header_id|>"],
|
| 40 |
+
echo=False
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
return response['choices'][0]['text'].strip()
|
| 44 |
|
| 45 |
+
# Create Gradio interface
|
| 46 |
+
demo = gr.ChatInterface(
|
| 47 |
+
chat,
|
| 48 |
+
title="🦙 My Fine-Tuned Llama-3.2-3B Chatbot",
|
| 49 |
+
description="""
|
| 50 |
+
**Model**: Llama-3.2-3B fine-tuned on FineTome-100k dataset
|
| 51 |
+
|
| 52 |
+
This chatbot uses a custom fine-tuned model, NOT ChatGPT.
|
| 53 |
+
|
| 54 |
+
Created for ID2223 Lab 2 at KTH.
|
| 55 |
+
""",
|
| 56 |
+
examples=[
|
| 57 |
+
"What model are you?",
|
| 58 |
+
"Explain machine learning in simple terms",
|
| 59 |
+
"Write a Python function to reverse a string",
|
| 60 |
+
"What is the weather like in Stockholm?"
|
|
|
|
| 61 |
],
|
| 62 |
+
theme="soft",
|
| 63 |
+
retry_btn="🔄 Retry",
|
| 64 |
+
undo_btn="↩️ Undo",
|
| 65 |
+
clear_btn="🗑️ Clear"
|
| 66 |
)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if __name__ == "__main__":
|
| 69 |
demo.launch()
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
### **Step 3: Update requirements.txt**
|
| 75 |
+
|
| 76 |
+
You also need to update the dependencies. Click on **`requirements.txt`** file and replace its contents with:
|
| 77 |
+
```
|
| 78 |
+
gradio
|
| 79 |
+
llama-cpp-python
|
| 80 |
+
huggingface-hub
|