File size: 1,810 Bytes
e9ecab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import gradio as gr
from transformers import pipeline
from dotenv import load_dotenv

# Load environment variables from .env
load_dotenv()
PASSWORD = os.getenv("ARMAAN_PASS")

# Load chatbot pipeline
chatbot = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta")

# Global system prompt (admin-editable)
system_prompt = "You are a helpful assistant."

# Chat function
def chat_fn(message, history):
    full_prompt = f"{system_prompt}\nUser: {message}\nBot:"
    response = chatbot(full_prompt, max_new_tokens=200, do_sample=True, temperature=0.7)[0]["generated_text"]
    reply = response.split("Bot:")[-1].strip()
    return reply

# Password check
def check_password(pass_input):
    if pass_input == PASSWORD:
        return gr.update(visible=True), ""
    else:
        return gr.update(visible=False), "Incorrect password"

# Prompt update
def update_prompt(new_prompt):
    global system_prompt
    system_prompt = new_prompt
    return "Prompt updated successfully."

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("# ChatGPT-Style Chatbot")

    with gr.Tab("Chat"):
        gr.ChatInterface(fn=chat_fn)

    with gr.Tab("Train"):
        with gr.Column():
            pass_input = gr.Textbox(label="Enter Admin Password", type="password")
            login_btn = gr.Button("Login")
            error_text = gr.Markdown("", visible=False)

            with gr.Group(visible=False) as admin_panel:
                new_prompt = gr.Textbox(label="New System Prompt", lines=4)
                update_btn = gr.Button("Update Prompt")
                success_msg = gr.Markdown("")

        login_btn.click(fn=check_password, inputs=pass_input, outputs=[admin_panel, error_text])
        update_btn.click(fn=update_prompt, inputs=new_prompt, outputs=success_msg)

demo.launch()