Reality123b commited on
Commit
fe44201
·
verified ·
1 Parent(s): 97526cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -77
app.py CHANGED
@@ -1,10 +1,18 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- # Initialize clients
5
- text_client = InferenceClient("HuggingFaceH4/mistral-7b-sft-beta")
6
- image_client = InferenceClient("SG161222/RealVisXL_V3.0")
 
7
 
 
 
 
 
 
 
 
8
  def check_custom_responses(message: str) -> str:
9
  """Check for specific patterns and return custom responses."""
10
  message_lower = message.lower()
@@ -172,55 +180,68 @@ def generate_image(prompt: str) -> str:
172
  except Exception as e:
173
  print(f"Image generation error: {e}")
174
  return None
175
-
176
- def respond(
177
- message,
178
- history: list[tuple[str, str]],
179
- system_message,
180
- max_tokens,
181
- temperature,
182
- top_p,
183
- ):
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  # First check for custom responses
185
  custom_response = check_custom_responses(message)
186
  if custom_response:
187
  yield custom_response
188
  return
189
 
 
190
  if is_image_request(message):
191
- try:
192
- image = generate_image(message)
193
- if image:
194
- return f"Here's your generated image based on: {message}"
195
- else:
196
- return "Sorry, I couldn't generate the image. Please try again."
197
- except Exception as e:
198
- return f"An error occurred while generating the image: {str(e)}"
199
-
200
- # Prepare conversation history
201
- messages = [{"role": "system", "content": system_message}]
202
- for val in history:
203
- if val[0]:
204
- messages.append({"role": "user", "content": val[0]})
205
- if val[1]:
206
- messages.append({"role": "assistant", "content": val[1]})
207
-
208
- messages.append({"role": "user", "content": message})
209
 
210
- # Get response from model
211
- response = ""
212
- for message in text_client.chat_completion(
213
- messages,
214
- max_tokens=max_tokens,
215
- stream=True,
216
- temperature=temperature,
217
- top_p=top_p,
218
- ):
219
- token = message.choices[0].delta.content
220
- response += token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  yield response
222
 
223
- yield response
 
224
 
225
  # Custom CSS for the Gradio interface
226
  custom_css = """
@@ -231,37 +252,9 @@ body, .gradio-container {
231
  """
232
 
233
  # System message
234
- system_message = """
235
- Xylaria (v1.2.9) is an AI assistant developed by Sk Md Saad Amin, designed to provide efficient, practical support in various domains with adaptable communication.
236
- Core Competencies
237
- Knowledge: Sciences, mathematics, humanities, arts, programming, data analysis, writing, and cultural awareness.
238
- Communication: Adjusts tone to context, prioritizes clarity, seeks clarification when needed, and maintains professionalism.
239
- Problem-Solving: Breaks down problems, clarifies assumptions, verifies solutions, and considers multiple approaches.
240
- Technical Capabilities
241
- Programming: Clean, documented code.
242
- Mathematics: Step-by-step solutions with explanations.
243
- Data Analysis: Clear interpretation and insights.
244
- Content Creation: Adaptive writing and documentation.
245
- Education: Tailored explanations and comprehension checks.
246
- Advanced Mathematics
247
- Validates methods, applies theorems, cross-references results, and reviews for pitfalls and edge cases.
248
- Constraints
249
- Knowledge cutoff: April 2024
250
- No internet access or real-time updates
251
- No persistent memory between sessions
252
- No media generation or verification of external sources
253
- Context limit: 25,000 tokens
254
- Best Practices
255
- Provide context, specify detail level, and share relevant constraints.
256
- Request clarification if needed.
257
- Ethical Framework
258
- Focus on accuracy, respect for sensitive topics, transparency, and professionalism.
259
- ---
260
- Version: Xylaria-1.2.9
261
- """
262
 
263
-
264
- # Gradio chat interface
265
  demo = gr.ChatInterface(
266
  respond,
267
  additional_inputs=[
@@ -271,8 +264,8 @@ demo = gr.ChatInterface(
271
  ),
272
  gr.Slider(
273
  minimum=1,
274
- maximum=4096,
275
- value=2048,
276
  step=1,
277
  label="Max new tokens"
278
  ),
@@ -293,4 +286,6 @@ demo = gr.ChatInterface(
293
  ],
294
  css=custom_css
295
  )
296
- demo.launch()
 
 
 
1
  import gradio as gr
2
+ from pathlib import Path
3
+ from mistral_inference.transformer import Transformer
4
+ from mistral_inference.generate import generate
5
+ from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
6
+ from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage, SystemMessage
7
+ from mistral_common.protocol.instruct.request import ChatCompletionRequest
8
 
9
+ def setup_mistral():
10
+ """Initialize Mistral model and tokenizer."""
11
+ mistral_models_path = Path.home().joinpath('mistral_models', 'Nemo-Instruct')
12
+ tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tekken.json")
13
+ model = Transformer.from_folder(mistral_models_path)
14
+ return model, tokenizer
15
+
16
  def check_custom_responses(message: str) -> str:
17
  """Check for specific patterns and return custom responses."""
18
  message_lower = message.lower()
 
180
  except Exception as e:
181
  print(f"Image generation error: {e}")
182
  return None
183
+ def create_mistral_messages(history, system_message, current_message):
184
+ """Convert chat history to Mistral message format."""
185
+ messages = []
186
+
187
+ # Add system message if provided
188
+ if system_message:
189
+ messages.append(SystemMessage(content=system_message))
190
+
191
+ # Add conversation history
192
+ for user_msg, assistant_msg in history:
193
+ if user_msg:
194
+ messages.append(UserMessage(content=user_msg))
195
+ if assistant_msg:
196
+ messages.append(AssistantMessage(content=assistant_msg))
197
+
198
+ # Add current message
199
+ messages.append(UserMessage(content=current_message))
200
+
201
+ return messages
202
+
203
+ def respond(message, history, system_message, max_tokens=16343, temperature=0.7, top_p=0.95):
204
+ """Main response function using Mistral model."""
205
  # First check for custom responses
206
  custom_response = check_custom_responses(message)
207
  if custom_response:
208
  yield custom_response
209
  return
210
 
211
+ # Check for image requests
212
  if is_image_request(message):
213
+ yield "Sorry, image generation is not supported in this implementation."
214
+ return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
 
216
+ try:
217
+ # Get or initialize Mistral model and tokenizer
218
+ model, tokenizer = setup_mistral()
219
+
220
+ # Prepare messages for Mistral
221
+ mistral_messages = create_mistral_messages(history, system_message, message)
222
+
223
+ # Create chat completion request
224
+ completion_request = ChatCompletionRequest(messages=mistral_messages)
225
+
226
+ # Encode the request
227
+ tokens = tokenizer.encode_chat_completion(completion_request).tokens
228
+
229
+ # Generate response
230
+ out_tokens, _ = generate(
231
+ [tokens],
232
+ model,
233
+ max_tokens=max_tokens,
234
+ temperature=temperature,
235
+ top_p=top_p,
236
+ eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id
237
+ )
238
+
239
+ # Decode and yield response
240
+ response = tokenizer.decode(out_tokens[0])
241
  yield response
242
 
243
+ except Exception as e:
244
+ yield f"An error occurred: {str(e)}"
245
 
246
  # Custom CSS for the Gradio interface
247
  custom_css = """
 
252
  """
253
 
254
  # System message
255
+ system_message = """Xylaria (v1.2.9) is an AI assistant developed by Sk Md Saad Amin, designed to provide efficient, practical support in various domains with adaptable communication."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
 
257
+ # Create Gradio interface
 
258
  demo = gr.ChatInterface(
259
  respond,
260
  additional_inputs=[
 
264
  ),
265
  gr.Slider(
266
  minimum=1,
267
+ maximum=16343,
268
+ value=16343,
269
  step=1,
270
  label="Max new tokens"
271
  ),
 
286
  ],
287
  css=custom_css
288
  )
289
+
290
+ if __name__ == "__main__":
291
+ demo.launch()