Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,167 @@
|
|
1 |
-
import gradio as gr
|
2 |
import os
|
|
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
model="Qwen/QwQ-32B-Preview",
|
17 |
-
messages=messages,
|
18 |
-
temperature=0.5,
|
19 |
-
max_tokens=10240,
|
20 |
-
top_p=0.7,
|
21 |
-
stream=True
|
22 |
-
)
|
23 |
-
|
24 |
-
response = ""
|
25 |
-
for chunk in stream:
|
26 |
-
response += chunk.choices[0].delta.content
|
27 |
-
return response
|
28 |
-
|
29 |
-
def chat_interface():
|
30 |
-
with gr.Blocks() as demo:
|
31 |
-
with gr.Row():
|
32 |
-
with gr.Column(scale=0.8):
|
33 |
-
input_textbox = gr.Textbox(
|
34 |
-
label="Type your message",
|
35 |
-
placeholder="Type to Xylaria...",
|
36 |
-
lines=1,
|
37 |
-
max_lines=3,
|
38 |
-
interactive=True,
|
39 |
-
elem_id="user-input",
|
40 |
-
show_label=False
|
41 |
-
)
|
42 |
-
with gr.Column(scale=0.2):
|
43 |
-
send_button = gr.Button("Send", elem_id="send-btn")
|
44 |
-
|
45 |
-
chat_output = gr.Chatbot(
|
46 |
-
elem_id="chat-box",
|
47 |
-
label="Xylaria 1.4 Senoa Chatbot",
|
48 |
-
show_label=False
|
49 |
)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
|
|
|
|
1 |
import os
|
2 |
+
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
|
5 |
+
class XylariaChat:
|
6 |
+
def __init__(self):
|
7 |
+
# Securely load HuggingFace token
|
8 |
+
self.hf_token = os.getenv("HF_TOKEN")
|
9 |
+
if not self.hf_token:
|
10 |
+
raise ValueError("HuggingFace token not found in environment variables")
|
11 |
+
|
12 |
+
# Initialize the inference client
|
13 |
+
self.client = InferenceClient(
|
14 |
+
model="Qwen/QwQ-32B-Preview",
|
15 |
+
api_key=self.hf_token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
)
|
17 |
|
18 |
+
# Initialize conversation history and persistent memory
|
19 |
+
self.conversation_history = []
|
20 |
+
self.persistent_memory = {}
|
21 |
+
|
22 |
+
# System prompt with more detailed instructions
|
23 |
+
self.system_prompt = """You are Xylaria 1.4 Senoa, an AI assistant developed by SK MD Saad Amin.
|
24 |
+
Key capabilities:
|
25 |
+
- Provide helpful and engaging responses
|
26 |
+
- Generate links for images when requested
|
27 |
+
- Maintain context across the conversation
|
28 |
+
- Be creative and supportive
|
29 |
+
- Remember key information shared by the user"""
|
30 |
+
|
31 |
+
def store_information(self, key, value):
|
32 |
+
"""Store important information in persistent memory"""
|
33 |
+
self.persistent_memory[key] = value
|
34 |
+
|
35 |
+
def retrieve_information(self, key):
|
36 |
+
"""Retrieve information from persistent memory"""
|
37 |
+
return self.persistent_memory.get(key)
|
38 |
|
39 |
+
def get_response(self, user_input):
|
40 |
+
# Prepare messages with conversation context and persistent memory
|
41 |
+
messages = [
|
42 |
+
{"role": "system", "content": self.system_prompt},
|
43 |
+
*self.conversation_history,
|
44 |
+
{"role": "user", "content": user_input}
|
45 |
+
]
|
46 |
|
47 |
+
# Add persistent memory context if available
|
48 |
+
if self.persistent_memory:
|
49 |
+
memory_context = "Remembered Information:\n" + "\n".join(
|
50 |
+
[f"{k}: {v}" for k, v in self.persistent_memory.items()]
|
51 |
+
)
|
52 |
+
messages.insert(1, {"role": "system", "content": memory_context})
|
53 |
+
|
54 |
+
# Generate response with streaming
|
55 |
+
try:
|
56 |
+
stream = self.client.chat.completions.create(
|
57 |
+
messages=messages,
|
58 |
+
temperature=0.5,
|
59 |
+
max_tokens=10240,
|
60 |
+
top_p=0.7,
|
61 |
+
stream=True
|
62 |
+
)
|
63 |
+
|
64 |
+
return stream
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
return f"Error generating response: {str(e)}"
|
68 |
+
|
69 |
+
def create_interface(self):
|
70 |
+
def streaming_response(message, chat_history):
|
71 |
+
# Clear input textbox
|
72 |
+
response_stream = self.get_response(message)
|
73 |
+
|
74 |
+
# If it's an error, return immediately
|
75 |
+
if isinstance(response_stream, str):
|
76 |
+
return "", chat_history + [[message, response_stream]]
|
77 |
+
|
78 |
+
# Prepare for streaming response
|
79 |
+
full_response = ""
|
80 |
+
updated_history = chat_history + [[message, ""]]
|
81 |
+
|
82 |
+
# Streaming output
|
83 |
+
for chunk in response_stream:
|
84 |
+
if chunk.choices[0].delta.content:
|
85 |
+
chunk_content = chunk.choices[0].delta.content
|
86 |
+
full_response += chunk_content
|
87 |
+
|
88 |
+
# Update the last message in chat history with partial response
|
89 |
+
updated_history[-1][1] = full_response
|
90 |
+
yield "", updated_history
|
91 |
+
|
92 |
+
# Update conversation history
|
93 |
+
self.conversation_history.append(
|
94 |
+
{"role": "user", "content": message}
|
95 |
+
)
|
96 |
+
self.conversation_history.append(
|
97 |
+
{"role": "assistant", "content": full_response}
|
98 |
+
)
|
99 |
+
|
100 |
+
# Limit conversation history to prevent token overflow
|
101 |
+
if len(self.conversation_history) > 10:
|
102 |
+
self.conversation_history = self.conversation_history[-10:]
|
103 |
+
|
104 |
+
with gr.Blocks(theme='soft') as demo:
|
105 |
+
# Chat interface with improved styling
|
106 |
+
with gr.Column():
|
107 |
+
chatbot = gr.Chatbot(
|
108 |
+
label="Xylaria 1.4 Senoa",
|
109 |
+
height=500,
|
110 |
+
show_copy_button=True
|
111 |
+
)
|
112 |
+
|
113 |
+
# Input row with improved layout
|
114 |
+
with gr.Row():
|
115 |
+
txt = gr.Textbox(
|
116 |
+
show_label=False,
|
117 |
+
placeholder="Type your message...",
|
118 |
+
container=False,
|
119 |
+
scale=4
|
120 |
+
)
|
121 |
+
btn = gr.Button("Send", scale=1)
|
122 |
+
|
123 |
+
# Clear history and memory buttons
|
124 |
+
clear = gr.Button("Clear Conversation")
|
125 |
+
clear_memory = gr.Button("Clear Memory")
|
126 |
+
|
127 |
+
# Submit functionality with streaming
|
128 |
+
btn.click(
|
129 |
+
fn=streaming_response,
|
130 |
+
inputs=[txt, chatbot],
|
131 |
+
outputs=[txt, chatbot]
|
132 |
+
)
|
133 |
+
txt.submit(
|
134 |
+
fn=streaming_response,
|
135 |
+
inputs=[txt, chatbot],
|
136 |
+
outputs=[txt, chatbot]
|
137 |
+
)
|
138 |
+
|
139 |
+
# Clear conversation history
|
140 |
+
clear.click(
|
141 |
+
fn=lambda: None,
|
142 |
+
inputs=None,
|
143 |
+
outputs=[chatbot],
|
144 |
+
queue=False
|
145 |
+
)
|
146 |
+
|
147 |
+
# Clear persistent memory
|
148 |
+
clear_memory.click(
|
149 |
+
fn=lambda: None,
|
150 |
+
inputs=None,
|
151 |
+
outputs=[],
|
152 |
+
queue=False
|
153 |
+
)
|
154 |
+
|
155 |
+
return demo
|
156 |
+
|
157 |
+
# Launch the interface
|
158 |
+
def main():
|
159 |
+
chat = XylariaChat()
|
160 |
+
interface = chat.create_interface()
|
161 |
+
interface.launch(
|
162 |
+
share=True, # Optional: create a public link
|
163 |
+
debug=True # Show detailed errors
|
164 |
+
)
|
165 |
|
166 |
+
if __name__ == "__main__":
|
167 |
+
main()
|