Reality123b commited on
Commit
0b53b16
·
verified ·
1 Parent(s): 0b8e032

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -21
app.py CHANGED
@@ -29,8 +29,8 @@ class XylariaChat:
29
  raise ValueError("HuggingFace token not found in environment variables")
30
 
31
  self.client = InferenceClient(
32
- model="Qwen/QwQ-32B-Preview",
33
- api_key=self.hf_token
34
  )
35
 
36
  self.image_api_url = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
@@ -442,6 +442,14 @@ class XylariaChat:
442
  if response.status_code == 200:
443
  image_bytes = response.content
444
  return image_bytes
 
 
 
 
 
 
 
 
445
  else:
446
  return f"Error generating image: {response.status_code} - {response.text}"
447
 
@@ -521,14 +529,17 @@ class XylariaChat:
521
  max_new_tokens = 16384 - input_tokens - 50
522
 
523
  max_new_tokens = min(max_new_tokens, 10020)
 
 
524
 
525
- stream = self.client.chat_completion(
526
- messages=messages,
527
- model="Qwen/QwQ-32B-Preview",
528
  temperature=0.7,
529
- max_tokens=max_new_tokens,
530
  top_p=0.9,
531
- stream=True
 
 
532
  )
533
 
534
  return stream
@@ -615,35 +626,64 @@ class XylariaChat:
615
 
616
  def create_interface(self):
617
  def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618
  if message.strip().lower().startswith("/image"):
619
- # Extract image generation prompt from the message
620
  image_prompt = message.strip().lower()[len("/image"):].strip()
621
  if not image_prompt:
622
- image_prompt = "A realistic image" # Default prompt
623
 
624
- # Generate image based on the extracted prompt
 
 
 
 
 
625
  image_bytes = self.generate_image(image_prompt)
626
 
627
  if isinstance(image_bytes, bytes):
628
  base64_image = base64.b64encode(image_bytes).decode("utf-8")
629
  image_html = f'<img src="data:image/png;base64,{base64_image}" alt="Generated Image" style="max-width: 100%; max-height: 400px;">'
630
 
631
- # Append the /image command and generated image to chat history
632
- chat_history.append([message, ""])
633
- chat_history.append(["", image_html])
634
 
635
- # Update conversation history
636
  self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
637
  self.conversation_history.append(ChatMessage(role="assistant", content=image_html).to_dict())
638
 
639
- # Update the chat history file
640
  self.save_chat()
641
  all_chats = self.load_all_chats()
642
  chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
643
 
644
  yield "", chat_history, None, None, gr.update(choices=chat_titles, visible=True)
645
  else:
646
- chat_history.append([message, image_bytes])
 
647
  yield "", chat_history, None, None, None
648
  return
649
 
@@ -669,18 +709,30 @@ class XylariaChat:
669
 
670
  full_response = ""
671
  updated_history = chat_history + [[message, ""]]
 
 
 
 
 
672
 
673
  try:
674
  for chunk in response_stream:
675
- if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
676
- chunk_content = chunk.choices[0].delta.content
677
- full_response += chunk_content
678
-
679
  updated_history[-1][1] = full_response
 
 
 
 
 
 
680
  yield "", updated_history, None, None, None
 
681
  except Exception as e:
682
  print(f"Streaming error: {e}")
683
  updated_history[-1][1] = f"Error during response: {e}"
 
684
  yield "", updated_history, None, None, None
685
  return
686
 
@@ -732,7 +784,7 @@ class XylariaChat:
732
  all_chats = self.load_all_chats()
733
  chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
734
  yield "", updated_history, None, None, gr.update(choices=chat_titles, visible=True)
735
-
736
  def load_selected_chat(chat_index, evt: gr.SelectData):
737
  if chat_index is not None:
738
  loaded_chat = self.load_chat(evt.index)
 
29
  raise ValueError("HuggingFace token not found in environment variables")
30
 
31
  self.client = InferenceClient(
32
+ model="Qwen/Qwen-32B-Preview", # Corrected model name
33
+ token=self.hf_token # Changed api_key to token
34
  )
35
 
36
  self.image_api_url = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large"
 
442
  if response.status_code == 200:
443
  image_bytes = response.content
444
  return image_bytes
445
+ elif response.status_code == 503:
446
+ error_message = response.json().get("error", "Unknown error")
447
+ if "estimated_time" in response.json():
448
+ estimated_time = response.json()["estimated_time"]
449
+ error_message += f" Estimated time to complete: {estimated_time:.2f} seconds"
450
+ else:
451
+ error_message += "The model is currently loading, please try again later"
452
+ return f"Error: {error_message}"
453
  else:
454
  return f"Error generating image: {response.status_code} - {response.text}"
455
 
 
529
  max_new_tokens = 16384 - input_tokens - 50
530
 
531
  max_new_tokens = min(max_new_tokens, 10020)
532
+
533
+ formatted_messages = self.messages_to_prompt(messages)
534
 
535
+ stream = self.client.text_generation(
536
+ prompt=formatted_messages,
537
+ max_new_tokens=max_new_tokens,
538
  temperature=0.7,
 
539
  top_p=0.9,
540
+ stream=True,
541
+ details=True,
542
+ do_sample=True
543
  )
544
 
545
  return stream
 
626
 
627
  def create_interface(self):
628
  def streaming_response(message, chat_history, image_filepath, math_ocr_image_path):
629
+ # Placeholder for image generation
630
+ loading_svg = """<svg width="256" height="256" viewBox="0 0 256 256" xmlns="http://www.w3.org/2000/svg">
631
+ <style>
632
+ rect {
633
+ animation: fillAnimation 3s ease-in-out infinite;
634
+ }
635
+ @keyframes fillAnimation {
636
+ 0% { fill: #626262; }
637
+ 50% { fill: #111111; }
638
+ 100% { fill: #626262; }
639
+ }
640
+ text {
641
+ font-family: 'Helvetica Neue', Arial, sans-serif;
642
+ font-weight: 300;
643
+ text-shadow: 0px 2px 4px rgba(0, 0, 0, 0.4);
644
+ }
645
+ </style>
646
+ <rect width="256" height="256" rx="20" fill="#888888" />
647
+ <text x="50%" y="50%" dominant-baseline="middle" text-anchor="middle" font-size="24" fill="white" opacity="0.8">
648
+ <tspan>creating your image</tspan>
649
+ <tspan x="50%" dy="1.2em">with xylaria iris</tspan>
650
+ </text>
651
+ </svg>"""
652
+
653
  if message.strip().lower().startswith("/image"):
654
+
655
  image_prompt = message.strip().lower()[len("/image"):].strip()
656
  if not image_prompt:
657
+ image_prompt = "A realistic image"
658
 
659
+
660
+ chat_history.append([message, ""])
661
+ chat_history.append(("", loading_svg))
662
+ yield "", chat_history, None, None, None
663
+
664
+
665
  image_bytes = self.generate_image(image_prompt)
666
 
667
  if isinstance(image_bytes, bytes):
668
  base64_image = base64.b64encode(image_bytes).decode("utf-8")
669
  image_html = f'<img src="data:image/png;base64,{base64_image}" alt="Generated Image" style="max-width: 100%; max-height: 400px;">'
670
 
671
+
672
+ chat_history[-1] = ("", image_html)
 
673
 
674
+
675
  self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
676
  self.conversation_history.append(ChatMessage(role="assistant", content=image_html).to_dict())
677
 
678
+
679
  self.save_chat()
680
  all_chats = self.load_all_chats()
681
  chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
682
 
683
  yield "", chat_history, None, None, gr.update(choices=chat_titles, visible=True)
684
  else:
685
+
686
+ chat_history[-1] = ("", image_bytes)
687
  yield "", chat_history, None, None, None
688
  return
689
 
 
709
 
710
  full_response = ""
711
  updated_history = chat_history + [[message, ""]]
712
+
713
+ if isinstance(response_stream, str):
714
+ updated_history = chat_history + [[message, response_stream]]
715
+ yield "", updated_history, None, None, None
716
+ return
717
 
718
  try:
719
  for chunk in response_stream:
720
+
721
+ if not chunk.token.special:
722
+ full_response += chunk.token.text
 
723
  updated_history[-1][1] = full_response
724
+
725
+
726
+
727
+ self.conversation_history.append(ChatMessage(role="user", content=message).to_dict())
728
+ self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict())
729
+
730
  yield "", updated_history, None, None, None
731
+
732
  except Exception as e:
733
  print(f"Streaming error: {e}")
734
  updated_history[-1][1] = f"Error during response: {e}"
735
+
736
  yield "", updated_history, None, None, None
737
  return
738
 
 
784
  all_chats = self.load_all_chats()
785
  chat_titles = [f"{chat['timestamp']}: {chat['conversation'][0]['content'][:30]}..." if len(chat['conversation']) > 0 and chat['conversation'][0]['content'] else f"{chat['timestamp']}: Empty Chat" for chat in all_chats]
786
  yield "", updated_history, None, None, gr.update(choices=chat_titles, visible=True)
787
+
788
  def load_selected_chat(chat_index, evt: gr.SelectData):
789
  if chat_index is not None:
790
  loaded_chat = self.load_chat(evt.index)