|
import os |
|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
|
|
class XylariaChat: |
|
def __init__(self): |
|
|
|
self.hf_token = os.getenv("HF_TOKEN") |
|
if not self.hf_token: |
|
raise ValueError("HuggingFace token not found in environment variables") |
|
|
|
|
|
self.client = InferenceClient( |
|
model="Qwen/QwQ-32B-Preview", |
|
api_key=self.hf_token |
|
) |
|
|
|
|
|
self.conversation_history = [] |
|
self.persistent_memory = {} |
|
|
|
|
|
self.system_prompt = """You are Xylaria 1.4 Senoa, an AI assistant developed by SK MD Saad Amin. |
|
Key capabilities: |
|
- Provide helpful and engaging responses |
|
- Generate links for images when requested |
|
- Maintain context across the conversation |
|
- Be creative and supportive |
|
- Remember key information shared by the user""" |
|
|
|
def store_information(self, key, value): |
|
"""Store important information in persistent memory""" |
|
self.persistent_memory[key] = value |
|
|
|
def retrieve_information(self, key): |
|
"""Retrieve information from persistent memory""" |
|
return self.persistent_memory.get(key) |
|
|
|
def get_response(self, user_input): |
|
|
|
messages = [ |
|
{"role": "system", "content": self.system_prompt}, |
|
*self.conversation_history, |
|
{"role": "user", "content": user_input} |
|
] |
|
|
|
|
|
if self.persistent_memory: |
|
memory_context = "Remembered Information:\n" + "\n".join( |
|
[f"{k}: {v}" for k, v in self.persistent_memory.items()] |
|
) |
|
messages.insert(1, {"role": "system", "content": memory_context}) |
|
|
|
|
|
try: |
|
stream = self.client.chat.completions.create( |
|
messages=messages, |
|
temperature=0.5, |
|
max_tokens=10240, |
|
top_p=0.7, |
|
stream=True |
|
) |
|
|
|
return stream |
|
|
|
except Exception as e: |
|
return f"Error generating response: {str(e)}" |
|
|
|
def create_interface(self): |
|
def streaming_response(message, chat_history): |
|
|
|
response_stream = self.get_response(message) |
|
|
|
|
|
if isinstance(response_stream, str): |
|
return "", chat_history + [[message, response_stream]] |
|
|
|
|
|
full_response = "" |
|
updated_history = chat_history + [[message, ""]] |
|
|
|
|
|
for chunk in response_stream: |
|
if chunk.choices[0].delta.content: |
|
chunk_content = chunk.choices[0].delta.content |
|
full_response += chunk_content |
|
|
|
|
|
updated_history[-1][1] = full_response |
|
yield "", updated_history |
|
|
|
|
|
self.conversation_history.append( |
|
{"role": "user", "content": message} |
|
) |
|
self.conversation_history.append( |
|
{"role": "assistant", "content": full_response} |
|
) |
|
|
|
|
|
if len(self.conversation_history) > 10: |
|
self.conversation_history = self.conversation_history[-10:] |
|
|
|
with gr.Blocks(theme='soft') as demo: |
|
|
|
with gr.Column(): |
|
chatbot = gr.Chatbot( |
|
label="Xylaria 1.4 Senoa", |
|
height=500, |
|
show_copy_button=True |
|
) |
|
|
|
|
|
with gr.Row(): |
|
txt = gr.Textbox( |
|
show_label=False, |
|
placeholder="Type your message...", |
|
container=False, |
|
scale=4 |
|
) |
|
btn = gr.Button("Send", scale=1) |
|
|
|
|
|
clear = gr.Button("Clear Conversation") |
|
clear_memory = gr.Button("Clear Memory") |
|
|
|
|
|
btn.click( |
|
fn=streaming_response, |
|
inputs=[txt, chatbot], |
|
outputs=[txt, chatbot] |
|
) |
|
txt.submit( |
|
fn=streaming_response, |
|
inputs=[txt, chatbot], |
|
outputs=[txt, chatbot] |
|
) |
|
|
|
|
|
clear.click( |
|
fn=lambda: None, |
|
inputs=None, |
|
outputs=[chatbot], |
|
queue=False |
|
) |
|
|
|
|
|
clear_memory.click( |
|
fn=lambda: None, |
|
inputs=None, |
|
outputs=[], |
|
queue=False |
|
) |
|
|
|
return demo |
|
|
|
|
|
def main(): |
|
chat = XylariaChat() |
|
interface = chat.create_interface() |
|
interface.launch( |
|
share=True, |
|
debug=True |
|
) |
|
|
|
if __name__ == "__main__": |
|
main() |