File size: 2,316 Bytes
7de273e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be62b69
7de273e
 
 
 
 
 
 
be62b69
7de273e
 
 
 
 
 
 
 
7cef515
7de273e
fce5a7a
7de273e
7cef515
 
 
 
 
 
 
 
 
7de273e
7cef515
 
7de273e
7cef515
7de273e
 
 
 
 
 
 
 
 
fce5a7a
7de273e
 
 
7cef515
7de273e
 
7cef515
7de273e
 
 
 
7cef515
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
64
65
66
67
68
69
70
71
72
# 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()