Reality123b commited on
Commit
caf6b1d
·
verified ·
1 Parent(s): 98f0993

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -124
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
 
4
 
5
  class XylariaChat:
6
  def __init__(self):
@@ -9,178 +10,101 @@ class XylariaChat:
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
- # Custom CSS for Inter font
105
- custom_css = """
106
- @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
107
-
108
- body, .gradio-container {
109
- font-family: 'Inter', sans-serif !important;
110
- }
111
 
112
- .chatbot-container .message {
113
- font-family: 'Inter', sans-serif !important;
114
- }
115
-
116
- .gradio-container input,
117
- .gradio-container textarea,
118
- .gradio-container button {
119
- font-family: 'Inter', sans-serif !important;
120
- }
121
- """
122
 
123
- with gr.Blocks(theme='soft', css=custom_css) as demo:
124
- # Chat interface with improved styling
125
- with gr.Column():
126
- chatbot = gr.Chatbot(
127
- label="Xylaria 1.4 Senoa",
128
- height=500,
129
- show_copy_button=True
130
- )
131
-
132
- # Input row with improved layout
133
- with gr.Row():
134
- txt = gr.Textbox(
135
- show_label=False,
136
- placeholder="Type your message...",
137
- container=False,
138
- scale=4
139
- )
140
- btn = gr.Button("Send", scale=1)
141
-
142
- # Clear history and memory buttons
143
- clear = gr.Button("Clear Conversation")
144
- clear_memory = gr.Button("Clear Memory")
145
-
146
- # Submit functionality with streaming
147
- btn.click(
148
- fn=streaming_response,
149
- inputs=[txt, chatbot],
150
- outputs=[txt, chatbot]
151
- )
152
- txt.submit(
153
- fn=streaming_response,
154
- inputs=[txt, chatbot],
155
- outputs=[txt, chatbot]
156
- )
157
 
158
- # Clear conversation history
159
- clear.click(
160
- fn=lambda: None,
161
- inputs=None,
162
- outputs=[chatbot],
163
- queue=False
164
- )
165
 
166
- # Clear persistent memory
167
- clear_memory.click(
168
- fn=lambda: None,
169
- inputs=None,
170
- outputs=[],
171
- queue=False
172
- )
173
-
174
  return demo
175
 
176
  # Launch the interface
177
  def main():
178
  chat = XylariaChat()
179
  interface = chat.create_interface()
180
- interface.launch(
181
- share=True, # Optional: create a public link
182
- debug=True # Show detailed errors
183
- )
184
 
185
  if __name__ == "__main__":
186
- main()
 
1
  import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
+ from PIL import Image
5
 
6
  class XylariaChat:
7
  def __init__(self):
 
10
  if not self.hf_token:
11
  raise ValueError("HuggingFace token not found in environment variables")
12
 
13
+ # Initialize the inference clients
14
+ self.chat_client = InferenceClient(
15
  model="Qwen/QwQ-32B-Preview",
16
  api_key=self.hf_token
17
  )
18
+ self.image_client = InferenceClient(
19
+ model="SG161222/RealVisXL_V4.0",
20
+ api_key=self.hf_token
21
+ )
22
 
23
  # Initialize conversation history and persistent memory
24
  self.conversation_history = []
25
  self.persistent_memory = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  def get_response(self, user_input):
28
+ """Get text-based response from the model."""
29
  messages = [
30
+ {"role": "system", "content": "Your name is Xylaria 1.4 Senoa, an advanced ai model developed by sk md saad amin"},
31
  *self.conversation_history,
32
  {"role": "user", "content": user_input}
33
  ]
34
 
 
 
 
 
 
 
 
 
35
  try:
36
+ response_stream = self.chat_client.chat.completions.create(
37
  messages=messages,
38
  temperature=0.5,
39
  max_tokens=10240,
40
  top_p=0.7,
41
  stream=True
42
  )
43
+ return response_stream
 
 
44
  except Exception as e:
45
  return f"Error generating response: {str(e)}"
46
 
47
+ def generate_image(self, prompt):
48
+ """Generate image based on prompt."""
49
+ try:
50
+ # Create an image from the prompt
51
+ image = self.image_client.text_to_image(prompt)
52
+ return image
53
+ except Exception as e:
54
+ return f"Error generating image: {str(e)}"
55
+
56
  def create_interface(self):
57
  def streaming_response(message, chat_history):
58
+ """Handle text response streaming."""
59
  response_stream = self.get_response(message)
 
 
60
  if isinstance(response_stream, str):
61
  return "", chat_history + [[message, response_stream]]
62
 
 
63
  full_response = ""
64
  updated_history = chat_history + [[message, ""]]
 
 
65
  for chunk in response_stream:
66
  if chunk.choices[0].delta.content:
67
+ full_response += chunk.choices[0].delta.content
 
 
 
68
  updated_history[-1][1] = full_response
69
  yield "", updated_history
70
 
71
+ self.conversation_history.append({"role": "user", "content": message})
72
+ self.conversation_history.append({"role": "assistant", "content": full_response})
 
 
 
 
 
 
 
73
  if len(self.conversation_history) > 10:
74
  self.conversation_history = self.conversation_history[-10:]
 
 
 
 
 
 
 
 
75
 
76
+ def generate_image_response(prompt):
77
+ """Handle image generation."""
78
+ if not prompt.strip():
79
+ return None
80
+ return self.generate_image(prompt)
 
 
 
 
 
81
 
82
+ with gr.Blocks() as demo:
83
+ chatbot = gr.Chatbot(label="Xylaria 1.4 Senoa", height=500)
84
+
85
+ with gr.Row():
86
+ txt = gr.Textbox(show_label=False, placeholder="Type your message...", scale=8)
87
+ send_btn = gr.Button("💬", scale=1)
88
+ img_btn = gr.Button("🖼️", scale=1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
+ clear_btn = gr.Button("Clear Conversation")
91
+ clear_memory_btn = gr.Button("Clear Memory")
92
+
93
+ send_btn.click(fn=streaming_response, inputs=[txt, chatbot], outputs=[txt, chatbot])
94
+ txt.submit(fn=streaming_response, inputs=[txt, chatbot], outputs=[txt, chatbot])
 
 
95
 
96
+ img_btn.click(fn=generate_image_response, inputs=txt, outputs=chatbot)
97
+
98
+ clear_btn.click(fn=lambda: None, inputs=None, outputs=chatbot)
99
+ clear_memory_btn.click(fn=lambda: None, inputs=None, outputs=[])
100
+
 
 
 
101
  return demo
102
 
103
  # Launch the interface
104
  def main():
105
  chat = XylariaChat()
106
  interface = chat.create_interface()
107
+ interface.launch(share=True, debug=True)
 
 
 
108
 
109
  if __name__ == "__main__":
110
+ main()