Spaces:
Running
Running
# app.py | |
import gradio as gr | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import torch | |
# ----------------------------- | |
# πΉ Load your fine-tuned model | |
# ----------------------------- | |
model_id = "kingmadhu1/Guru-ILackMini-Knowledge" # Hugging Face repo | |
tokenizer = AutoTokenizer.from_pretrained(model_id) | |
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto") | |
# ----------------------------- | |
# πΉ Chat function | |
# ----------------------------- | |
def chat_with_guru(prompt, max_new_tokens=120): | |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
outputs = model.generate( | |
**inputs, | |
max_new_tokens=max_new_tokens, | |
do_sample=True, | |
temperature=0.7, | |
top_p=0.9, | |
repetition_penalty=1.2, | |
pad_token_id=tokenizer.eos_token_id | |
) | |
return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# ----------------------------- | |
# πΉ Respond function for Gradio | |
# ----------------------------- | |
def respond(user_input, chat_history): | |
# Build system prompt + conversation history | |
system_prompt = ( | |
"You are Guru, an instructor AI.\n" | |
) | |
# Include previous chat as context | |
conversation_context = "" | |
for q, a in chat_history: | |
conversation_context += f"User: {q}\nGuru: {a}\n" | |
prompt = system_prompt + conversation_context + f"User: {user_input}\nGuru:" | |
# Generate response | |
response = chat_with_guru(prompt) | |
# Update history | |
chat_history.append((user_input, response)) | |
return chat_history, chat_history | |
# ----------------------------- | |
# πΉ Gradio Interface | |
# ----------------------------- | |
with gr.Blocks() as demo: | |
chat_history = gr.State([]) | |
with gr.Column(): | |
gr.Markdown("## Guru AI β I am Still Learning") | |
chatbot = gr.Chatbot() | |
user_input = gr.Textbox(placeholder="Ask Guru anything...") | |
send_btn = gr.Button("Send") | |
clear_btn = gr.Button("Clear Chat") | |
send_btn.click(respond, inputs=[user_input, chat_history], outputs=[chatbot, chat_history]) | |
clear_btn.click(lambda: ([], []), inputs=None, outputs=[chatbot, chat_history]) | |
# ----------------------------- | |
# πΉ Launch app | |
# ----------------------------- | |
demo.launch() |