Spaces:
Runtime error
Runtime error
File size: 6,491 Bytes
f3fac44 716a943 f3fac44 716a943 |
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
import gradio as gr
import json
from pathlib import Path
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
# Default system prompt for the chat interface
DEFAULT_SYSTEM_PROMPT = """You are DeepThink, a helpful and knowledgeable AI assistant. You aim to provide accurate,
informative, and engaging responses while maintaining a professional and friendly demeanor."""
class ChatInterface:
"""Main chat interface handler with memory and parameter management"""
def __init__(self):
"""Initialize the chat interface with default settings"""
self.model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name)
self.model = AutoModelForCausalLM.from_pretrained(self.model_name)
self.chat_history = []
self.system_prompt = DEFAULT_SYSTEM_PROMPT
def load_context_from_json(self, file_obj):
"""Load additional context from a JSON file"""
if file_obj is None:
return "No file uploaded", self.system_prompt
try:
content = json.load(file_obj)
if "system_prompt" in content:
self.system_prompt = content["system_prompt"]
return "Context loaded successfully!", self.system_prompt
except Exception as e:
return f"Error loading context: {str(e)}", self.system_prompt
def generate_response(self, message, temperature, max_length, top_p, presence_penalty, frequency_penalty):
"""Generate AI response with given parameters"""
# Format the input with system prompt and chat history
conversation = f"System: {self.system_prompt}\n\n"
for msg in self.chat_history:
conversation += f"Human: {msg[0]}\nAssistant: {msg[1]}\n\n"
conversation += f"Human: {message}\nAssistant:"
# Generate response with specified parameters
inputs = self.tokenizer(conversation, return_tensors="pt")
outputs = self.model.generate(
inputs["input_ids"],
max_length=max_length,
temperature=temperature,
top_p=top_p,
presence_penalty=presence_penalty,
frequency_penalty=frequency_penalty,
)
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract assistant's response and update chat history
response = response.split("Assistant:")[-1].strip()
self.chat_history.append((message, response))
return response, self.format_chat_history()
def format_chat_history(self):
"""Format chat history for display"""
return [(f"User: {msg[0]}", f"Assistant: {msg[1]}") for msg in self.chat_history]
def clear_history(self):
"""Clear the chat history"""
self.chat_history = []
return self.format_chat_history()
# Initialize the chat interface
chat_interface = ChatInterface()
# Create the Gradio interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
with gr.Row():
with gr.Column(scale=2):
# Main chat interface
chatbot = gr.Chatbot(
label="Chat History",
height=600,
show_label=True,
)
with gr.Row():
message = gr.Textbox(
label="Your message",
placeholder="Type your message here...",
lines=2
)
submit_btn = gr.Button("Send", variant="primary")
with gr.Column(scale=1):
# System settings and parameters
with gr.Group(label="System Configuration"):
system_prompt = gr.Textbox(
label="System Prompt",
value=DEFAULT_SYSTEM_PROMPT,
lines=4
)
context_file = gr.File(
label="Upload Context JSON",
file_types=[".json"]
)
upload_button = gr.Button("Load Context")
context_status = gr.Textbox(label="Context Status", interactive=False)
with gr.Group(label="Generation Parameters"):
temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=0.7,
step=0.1,
label="Temperature"
)
max_length = gr.Slider(
minimum=50,
maximum=2000,
value=500,
step=50,
label="Max Length"
)
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.9,
step=0.1,
label="Top P"
)
presence_penalty = gr.Slider(
minimum=0.0,
maximum=2.0,
value=0.0,
step=0.1,
label="Presence Penalty"
)
frequency_penalty = gr.Slider(
minimum=0.0,
maximum=2.0,
value=0.0,
step=0.1,
label="Frequency Penalty"
)
clear_btn = gr.Button("Clear Chat History")
# Event handlers
def submit_message(message, temperature, max_length, top_p, presence_penalty, frequency_penalty):
response, history = chat_interface.generate_response(
message, temperature, max_length, top_p, presence_penalty, frequency_penalty
)
return "", history
submit_btn.click(
submit_message,
inputs=[message, temperature, max_length, top_p, presence_penalty, frequency_penalty],
outputs=[message, chatbot]
)
message.submit(
submit_message,
inputs=[message, temperature, max_length, top_p, presence_penalty, frequency_penalty],
outputs=[message, chatbot]
)
clear_btn.click(
lambda: (chat_interface.clear_history(), ""),
outputs=[chatbot, message]
)
upload_button.click(
chat_interface.load_context_from_json,
inputs=[context_file],
outputs=[context_status, system_prompt]
)
# Launch the interface
demo.launch() |