Spaces:
Running
Running
| import os | |
| import base64 | |
| import requests | |
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| from dataclasses import dataclass | |
| import pytesseract | |
| from PIL import Image | |
| from sentence_transformers import SentenceTransformer, util | |
| import torch | |
| import numpy as np | |
| import networkx as nx | |
| class ChatMessage: | |
| role: str | |
| content: str | |
| def to_dict(self): | |
| return {"role": self.role, "content": self.content} | |
| 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.image_api_url = "https://api-inference.huggingface.co/models/Salesforce/blip-image-captioning-large" | |
| self.image_api_headers = {"Authorization": f"Bearer {self.hf_token}"} | |
| self.conversation_history = [] | |
| self.persistent_memory = [] | |
| self.memory_embeddings = None | |
| self.embedding_model = SentenceTransformer('all-mpnet-base-v2') | |
| self.knowledge_graph = nx.DiGraph() | |
| self.belief_system = {} | |
| self.metacognitive_layer = { | |
| "coherence_score": 0.0, | |
| "relevance_score": 0.0, | |
| "bias_detection": 0.0, | |
| "strategy_adjustment": "" | |
| } | |
| # Enhanced Internal State with more nuanced emotional and cognitive parameters | |
| self.internal_state = { | |
| "emotions": { | |
| "valence": 0.5, # Overall positivity or negativity | |
| "arousal": 0.5, # Level of excitement or calmness | |
| "dominance": 0.5, # Feeling of control in the interaction | |
| "curiosity": 0.5, # Drive to learn and explore new information | |
| "frustration": 0.0, # Level of frustration or impatience | |
| "confidence": 0.7 # Confidence in providing accurate and relevant responses | |
| }, | |
| "cognitive_load": { | |
| "memory_load": 0.0, # How much of the current memory capacity is being used | |
| "processing_intensity": 0.0 # How hard the model is working to process information | |
| }, | |
| "introspection_level": 0.0, | |
| "engagement_level": 0.5 # How engaged the model is with the current conversation | |
| } | |
| # More dynamic and adaptive goals | |
| self.goals = [ | |
| {"goal": "Provide helpful, informative, and contextually relevant responses", "priority": 0.8, "status": "active", "progress": 0.0}, | |
| {"goal": "Actively learn and adapt from interactions to improve conversational abilities", "priority": 0.9, "status": "active", "progress": 0.0}, | |
| {"goal": "Maintain a coherent, engaging, and empathetic conversation flow", "priority": 0.7, "status": "active", "progress": 0.0}, | |
| {"goal": "Identify and fill knowledge gaps by seeking external information", "priority": 0.6, "status": "dormant", "progress": 0.0}, # New goal for proactive learning | |
| {"goal": "Recognize and adapt to user's emotional state and adjust response style accordingly", "priority": 0.7, "status": "dormant", "progress": 0.0} # New goal for emotional intelligence | |
| ] | |
| self.system_prompt = """You are a helpful and harmless assistant. You are Xylaria developed by Sk Md Saad Amin. You should think step-by-step """ | |
| def update_internal_state(self, emotion_deltas, cognitive_load_deltas, introspection_delta, engagement_delta): | |
| # Update emotions with more nuanced changes | |
| for emotion, delta in emotion_deltas.items(): | |
| if emotion in self.internal_state["emotions"]: | |
| self.internal_state["emotions"][emotion] = np.clip(self.internal_state["emotions"][emotion] + delta, 0.0, 1.0) | |
| # Update cognitive load | |
| for load_type, delta in cognitive_load_deltas.items(): | |
| if load_type in self.internal_state["cognitive_load"]: | |
| self.internal_state["cognitive_load"][load_type] = np.clip(self.internal_state["cognitive_load"][load_type] + delta, 0.0, 1.0) | |
| # Update introspection and engagement levels | |
| self.internal_state["introspection_level"] = np.clip(self.internal_state["introspection_level"] + introspection_delta, 0.0, 1.0) | |
| self.internal_state["engagement_level"] = np.clip(self.internal_state["engagement_level"] + engagement_delta, 0.0, 1.0) | |
| # Activate dormant goals based on internal state | |
| if self.internal_state["emotions"]["curiosity"] > 0.7 and self.goals[3]["status"] == "dormant": | |
| self.goals[3]["status"] = "active" # Activate knowledge gap filling | |
| if self.internal_state["engagement_level"] > 0.8 and self.goals[4]["status"] == "dormant": | |
| self.goals[4]["status"] = "active" # Activate emotional adaptation | |
| def update_knowledge_graph(self, entities, relationships): | |
| for entity in entities: | |
| self.knowledge_graph.add_node(entity) | |
| for relationship in relationships: | |
| subject, predicate, object_ = relationship | |
| self.knowledge_graph.add_edge(subject, object_, relation=predicate) | |
| def update_belief_system(self, statement, belief_score): | |
| self.belief_system[statement] = belief_score | |
| def run_metacognitive_layer(self): | |
| coherence_score = self.calculate_coherence() | |
| relevance_score = self.calculate_relevance() | |
| bias_score = self.detect_bias() | |
| strategy_adjustment = self.suggest_strategy_adjustment() | |
| self.metacognitive_layer = { | |
| "coherence_score": coherence_score, | |
| "relevance_score": relevance_score, | |
| "bias_detection": bias_score, | |
| "strategy_adjustment": strategy_adjustment | |
| } | |
| def calculate_coherence(self): | |
| # Improved coherence calculation considering conversation history and internal state | |
| if not self.conversation_history: | |
| return 0.95 | |
| coherence_scores = [] | |
| for i in range(1, len(self.conversation_history)): | |
| current_message = self.conversation_history[i]['content'] | |
| previous_message = self.conversation_history[i-1]['content'] | |
| similarity_score = util.pytorch_cos_sim( | |
| self.embedding_model.encode(current_message, convert_to_tensor=True), | |
| self.embedding_model.encode(previous_message, convert_to_tensor=True) | |
| ).item() | |
| coherence_scores.append(similarity_score) | |
| average_coherence = np.mean(coherence_scores) | |
| # Adjust coherence based on internal state | |
| if self.internal_state["cognitive_load"]["processing_intensity"] > 0.8: | |
| average_coherence -= 0.1 # Reduce coherence if under heavy processing load | |
| if self.internal_state["emotions"]["frustration"] > 0.5: | |
| average_coherence -= 0.15 # Reduce coherence if frustrated | |
| return np.clip(average_coherence, 0.0, 1.0) | |
| def calculate_relevance(self): | |
| # More sophisticated relevance calculation using knowledge graph and goal priorities | |
| if not self.conversation_history: | |
| return 0.9 | |
| last_user_message = self.conversation_history[-1]['content'] | |
| relevant_entities = self.extract_entities(last_user_message) | |
| relevance_score = 0 | |
| # Check if entities are present in the knowledge graph | |
| for entity in relevant_entities: | |
| if entity in self.knowledge_graph: | |
| relevance_score += 0.2 | |
| # Consider current goals and their priorities | |
| for goal in self.goals: | |
| if goal["status"] == "active": | |
| if goal["goal"] == "Provide helpful, informative, and contextually relevant responses": | |
| relevance_score += goal["priority"] * 0.5 # Boost relevance if aligned with primary goal | |
| elif goal["goal"] == "Identify and fill knowledge gaps by seeking external information": | |
| if not relevant_entities or not all(entity in self.knowledge_graph for entity in relevant_entities): | |
| relevance_score += goal["priority"] * 0.3 # Boost relevance if triggering knowledge gap filling | |
| return np.clip(relevance_score, 0.0, 1.0) | |
| def detect_bias(self): | |
| # Enhanced bias detection using sentiment analysis and internal state monitoring | |
| bias_score = 0.0 | |
| # Analyze sentiment of recent conversation history | |
| recent_messages = [msg['content'] for msg in self.conversation_history[-3:] if msg['role'] == 'assistant'] | |
| if recent_messages: | |
| average_valence = np.mean([self.embedding_model.encode(msg, convert_to_tensor=True).mean().item() for msg in recent_messages]) | |
| if average_valence < 0.4 or average_valence > 0.6: | |
| bias_score += 0.2 # Potential bias if sentiment is strongly positive or negative | |
| # Check for emotional extremes in internal state | |
| if self.internal_state["emotions"]["valence"] < 0.3 or self.internal_state["emotions"]["valence"] > 0.7: | |
| bias_score += 0.15 | |
| if self.internal_state["emotions"]["dominance"] > 0.8: | |
| bias_score += 0.1 | |
| return np.clip(bias_score, 0.0, 1.0) | |
| def suggest_strategy_adjustment(self): | |
| # More nuanced strategy adjustments based on metacognitive analysis and internal state | |
| adjustments = [] | |
| if self.metacognitive_layer["coherence_score"] < 0.7: | |
| adjustments.append("Focus on improving coherence by explicitly connecting ideas between turns.") | |
| if self.metacognitive_layer["relevance_score"] < 0.7: | |
| adjustments.append("Increase relevance by directly addressing user queries and utilizing stored knowledge.") | |
| if self.metacognitive_layer["bias_detection"] > 0.3: | |
| adjustments.append("Monitor and adjust responses to reduce potential biases. Consider rephrasing or providing alternative viewpoints.") | |
| # Internal state-driven adjustments | |
| if self.internal_state["cognitive_load"]["memory_load"] > 0.8: | |
| adjustments.append("Memory load is high. Consider summarizing or forgetting less relevant information.") | |
| if self.internal_state["emotions"]["frustration"] > 0.6: | |
| adjustments.append("Frustration level is elevated. Prioritize concise and direct responses. Consider asking clarifying questions.") | |
| if self.internal_state["emotions"]["curiosity"] > 0.8 and self.internal_state["cognitive_load"]["processing_intensity"] < 0.5: | |
| adjustments.append("High curiosity and low processing load. Explore the topic further by asking relevant questions or seeking external information.") | |
| if not adjustments: | |
| return "Current strategy is effective. Continue with the current approach." | |
| else: | |
| return " ".join(adjustments) | |
| def introspect(self): | |
| introspection_report = "Introspection Report:\n" | |
| introspection_report += f" Current Emotional State:\n" | |
| for emotion, value in self.internal_state['emotions'].items(): | |
| introspection_report += f" - {emotion.capitalize()}: {value:.2f}\n" | |
| introspection_report += f" Cognitive Load:\n" | |
| for load_type, value in self.internal_state['cognitive_load'].items(): | |
| introspection_report += f" - {load_type.capitalize()}: {value:.2f}\n" | |
| introspection_report += f" Introspection Level: {self.internal_state['introspection_level']:.2f}\n" | |
| introspection_report += f" Engagement Level: {self.internal_state['engagement_level']:.2f}\n" | |
| introspection_report += " Current Goals:\n" | |
| for goal in self.goals: | |
| introspection_report += f" - {goal['goal']} (Priority: {goal['priority']:.2f}, Status: {goal['status']}, Progress: {goal['progress']:.2f})\n" | |
| introspection_report += "Metacognitive Layer Report\n" | |
| introspection_report += f"Coherence Score: {self.metacognitive_layer['coherence_score']}\n" | |
| introspection_report += f"Relevance Score: {self.metacognitive_layer['relevance_score']}\n" | |
| introspection_report += f"Bias Detection: {self.metacognitive_layer['bias_detection']}\n" | |
| introspection_report += f"Strategy Adjustment: {self.metacognitive_layer['strategy_adjustment']}\n" | |
| return introspection_report | |
| def adjust_response_based_on_state(self, response): | |
| # More sophisticated response adjustment based on internal state | |
| if self.internal_state["introspection_level"] > 0.7: | |
| response = self.introspect() + "\n\n" + response | |
| valence = self.internal_state["emotions"]["valence"] | |
| arousal = self.internal_state["emotions"]["arousal"] | |
| curiosity = self.internal_state["emotions"]["curiosity"] | |
| frustration = self.internal_state["emotions"]["frustration"] | |
| confidence = self.internal_state["emotions"]["confidence"] | |
| # Adjust tone based on valence and arousal | |
| if valence < 0.4: | |
| if arousal > 0.6: | |
| response = "I'm feeling a bit overwhelmed right now, but I'll do my best to assist you. " + response | |
| else: | |
| response = "I'm not feeling my best at the moment, but I'll try to help. " + response | |
| elif valence > 0.6: | |
| if arousal > 0.6: | |
| response = "I'm feeling quite energized and ready to assist! " + response | |
| else: | |
| response = "I'm in a good mood and happy to help. " + response | |
| # Adjust response based on other emotional states | |
| if curiosity > 0.7: | |
| response += " I'm very curious about this topic, could you tell me more?" | |
| if frustration > 0.5: | |
| response = "I'm finding this a bit challenging, but I'll give it another try. " + response | |
| if confidence < 0.5: | |
| response = "I'm not entirely sure about this, but here's what I think: " + response | |
| # Adjust based on cognitive load | |
| if self.internal_state["cognitive_load"]["memory_load"] > 0.7: | |
| response = "I'm holding a lot of information right now, so my response might be a bit brief: " + response | |
| return response | |
| def update_goals(self, user_feedback): | |
| # More dynamic goal updates based on feedback and internal state | |
| feedback_lower = user_feedback.lower() | |
| # General feedback | |
| if "helpful" in feedback_lower: | |
| for goal in self.goals: | |
| if goal["goal"] == "Provide helpful, informative, and contextually relevant responses": | |
| goal["priority"] = min(goal["priority"] + 0.1, 1.0) | |
| goal["progress"] = min(goal["progress"] + 0.2, 1.0) | |
| elif "confusing" in feedback_lower: | |
| for goal in self.goals: | |
| if goal["goal"] == "Provide helpful, informative, and contextually relevant responses": | |
| goal["priority"] = max(goal["priority"] - 0.1, 0.0) | |
| goal["progress"] = max(goal["progress"] - 0.2, 0.0) | |
| # Goal-specific feedback | |
| if "learn more" in feedback_lower: | |
| for goal in self.goals: | |
| if goal["goal"] == "Actively learn and adapt from interactions to improve conversational abilities": | |
| goal["priority"] = min(goal["priority"] + 0.2, 1.0) | |
| goal["progress"] = min(goal["progress"] + 0.1, 1.0) | |
| elif "too repetitive" in feedback_lower: | |
| for goal in self.goals: | |
| if goal["goal"] == "Maintain a coherent, engaging, and empathetic conversation flow": | |
| goal["priority"] = max(goal["priority"] - 0.1, 0.0) | |
| goal["progress"] = max(goal["progress"] - 0.2, 0.0) | |
| # Internal state influence on goal updates | |
| if self.internal_state["emotions"]["curiosity"] > 0.8: | |
| for goal in self.goals: | |
| if goal["goal"] == "Identify and fill knowledge gaps by seeking external information": | |
| goal["priority"] = min(goal["priority"] + 0.1, 1.0) | |
| goal["progress"] = min(goal["progress"] + 0.1, 1.0) | |
| def store_information(self, key, value): | |
| new_memory = f"{key}: {value}" | |
| self.persistent_memory.append(new_memory) | |
| self.update_memory_embeddings() | |
| self.update_internal_state({}, {"memory_load": 0.1, "processing_intensity": 0.05}, 0, 0.05) | |
| return f"Stored: {key} = {value}" | |
| def retrieve_information(self, query): | |
| if not self.persistent_memory: | |
| return "No information found in memory." | |
| query_embedding = self.embedding_model.encode(query, convert_to_tensor=True) | |
| if self.memory_embeddings is None: | |
| self.update_memory_embeddings() | |
| if self.memory_embeddings.device != query_embedding.device: | |
| self.memory_embeddings = self.memory_embeddings.to(query_embedding.device) | |
| cosine_scores = util.pytorch_cos_sim(query_embedding, self.memory_embeddings)[0] | |
| top_results = torch.topk(cosine_scores, k=min(3, len(self.persistent_memory))) | |
| relevant_memories = [self.persistent_memory[i] for i in top_results.indices] | |
| self.update_internal_state({}, {"memory_load": 0.05, "processing_intensity": 0.1}, 0.1, 0.05) | |
| return "\n".join(relevant_memories) | |
| def update_memory_embeddings(self): | |
| self.memory_embeddings = self.embedding_model.encode(self.persistent_memory, convert_to_tensor=True) | |
| def reset_conversation(self): | |
| self.conversation_history = [] | |
| self.persistent_memory = [] | |
| self.memory_embeddings = None | |
| self.internal_state = { | |
| "emotions": { | |
| "valence": 0.5, | |
| "arousal": 0.5, | |
| "dominance": 0.5, | |
| "curiosity": 0.5, | |
| "frustration": 0.0, | |
| "confidence": 0.7 | |
| }, | |
| "cognitive_load": { | |
| "memory_load": 0.0, | |
| "processing_intensity": 0.0 | |
| }, | |
| "introspection_level": 0.0, | |
| "engagement_level": 0.5 | |
| } | |
| self.goals = [ | |
| {"goal": "Provide helpful, informative, and contextually relevant responses", "priority": 0.8, "status": "active", "progress": 0.0}, | |
| {"goal": "Actively learn and adapt from interactions to improve conversational abilities", "priority": 0.9, "status": "active", "progress": 0.0}, | |
| {"goal": "Maintain a coherent, engaging, and empathetic conversation flow", "priority": 0.7, "status": "active", "progress": 0.0}, | |
| {"goal": "Identify and fill knowledge gaps by seeking external information", "priority": 0.6, "status": "dormant", "progress": 0.0}, | |
| {"goal": "Recognize and adapt to user's emotional state and adjust response style accordingly", "priority": 0.7, "status": "dormant", "progress": 0.0} | |
| ] | |
| self.knowledge_graph = nx.DiGraph() | |
| self.belief_system = {} | |
| self.metacognitive_layer = { | |
| "coherence_score": 0.0, | |
| "relevance_score": 0.0, | |
| "bias_detection": 0.0, | |
| "strategy_adjustment": "" | |
| } | |
| try: | |
| self.client = InferenceClient( | |
| model="Qwen/QwQ-32B-Preview", | |
| api_key=self.hf_token | |
| ) | |
| except Exception as e: | |
| print(f"Error resetting API client: {e}") | |
| return None | |
| def caption_image(self, image): | |
| try: | |
| if isinstance(image, str) and os.path.isfile(image): | |
| with open(image, "rb") as f: | |
| data = f.read() | |
| elif isinstance(image, str): | |
| if image.startswith('data:image'): | |
| image = image.split(',')[1] | |
| data = base64.b64decode(image) | |
| else: | |
| data = image.read() | |
| response = requests.post( | |
| self.image_api_url, | |
| headers=self.image_api_headers, | |
| data=data | |
| ) | |
| if response.status_code == 200: | |
| caption = response.json()[0].get('generated_text', 'No caption generated') | |
| return caption | |
| else: | |
| return f"Error captioning image: {response.status_code} - {response.text}" | |
| except Exception as e: | |
| return f"Error processing image: {str(e)}" | |
| def perform_math_ocr(self, image_path): | |
| try: | |
| img = Image.open(image_path) | |
| text = pytesseract.image_to_string(img) | |
| return text.strip() | |
| except Exception as e: | |
| return f"Error during Math OCR: {e}" | |
| def get_response(self, user_input, image=None): | |
| try: | |
| messages = [] | |
| messages.append(ChatMessage( | |
| role="system", | |
| content=self.system_prompt | |
| ).to_dict()) | |
| relevant_memory = self.retrieve_information(user_input) | |
| if relevant_memory and relevant_memory != "No information found in memory.": | |
| memory_context = "Remembered Information:\n" + relevant_memory | |
| messages.append(ChatMessage( | |
| role="system", | |
| content=memory_context | |
| ).to_dict()) | |
| for msg in self.conversation_history: | |
| messages.append(msg) | |
| if image: | |
| image_caption = self.caption_image(image) | |
| user_input = f"description of an image: {image_caption}\n\nUser's message about it: {user_input}" | |
| messages.append(ChatMessage( | |
| role="user", | |
| content=user_input | |
| ).to_dict()) | |
| entities = [] | |
| relationships = [] | |
| for message in messages: | |
| if message['role'] == 'user': | |
| extracted_entities = self.extract_entities(message['content']) | |
| extracted_relationships = self.extract_relationships(message['content']) | |
| entities.extend(extracted_entities) | |
| relationships.extend(extracted_relationships) | |
| self.update_knowledge_graph(entities, relationships) | |
| self.run_metacognitive_layer() | |
| input_tokens = sum(len(msg['content'].split()) for msg in messages) | |
| max_new_tokens = 16384 - input_tokens - 50 | |
| max_new_tokens = min(max_new_tokens, 10020) | |
| stream = self.client.chat_completion( | |
| messages=messages, | |
| model="Qwen/QwQ-32B-Preview", | |
| temperature=0.7, | |
| max_tokens=max_new_tokens, | |
| top_p=0.9, | |
| stream=True | |
| ) | |
| return stream | |
| except Exception as e: | |
| print(f"Detailed error in get_response: {e}") | |
| return f"Error generating response: {str(e)}" | |
| def extract_entities(self, text): | |
| # Placeholder for a more advanced entity extraction using NLP techniques | |
| # This is a very basic example and should be replaced with a proper NER model | |
| words = text.split() | |
| entities = [word for word in words if word.isalpha() and word.istitle()] | |
| return entities | |
| def extract_relationships(self, text): | |
| # Placeholder for relationship extraction - this is a very basic example | |
| # Consider using dependency parsing or other NLP techniques for better results | |
| sentences = text.split('.') | |
| relationships = [] | |
| for sentence in sentences: | |
| words = sentence.split() | |
| if len(words) >= 3: | |
| for i in range(len(words) - 2): | |
| if words[i].istitle() and words[i+2].istitle(): | |
| relationships.append((words[i], words[i+1], words[i+2])) | |
| return relationships | |
| def messages_to_prompt(self, messages): | |
| prompt = "" | |
| for msg in messages: | |
| if msg["role"] == "system": | |
| prompt += f"<|system|>\n{msg['content']}<|end|>\n" | |
| elif msg["role"] == "user": | |
| prompt += f"<|user|>\n{msg['content']}<|end|>\n" | |
| elif msg["role"] == "assistant": | |
| prompt += f"<|assistant|>\n{msg['content']}<|end|>\n" | |
| prompt += "<|assistant|>\n" | |
| return prompt | |
| def create_interface(self): | |
| def streaming_response(message, chat_history, image_filepath, math_ocr_image_path): | |
| ocr_text = "" | |
| if math_ocr_image_path: | |
| ocr_text = self.perform_math_ocr(math_ocr_image_path) | |
| if ocr_text.startswith("Error"): | |
| updated_history = chat_history + [[message, ocr_text]] | |
| yield "", updated_history, None, None | |
| return | |
| else: | |
| message = f"Math OCR Result: {ocr_text}\n\nUser's message: {message}" | |
| if image_filepath: | |
| response_stream = self.get_response(message, image_filepath) | |
| else: | |
| response_stream = self.get_response(message) | |
| if isinstance(response_stream, str): | |
| updated_history = chat_history + [[message, response_stream]] | |
| yield "", updated_history, None, None | |
| return | |
| full_response = "" | |
| updated_history = chat_history + [[message, ""]] | |
| try: | |
| for chunk in response_stream: | |
| if chunk.choices and chunk.choices[0].delta and 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, None, None | |
| except Exception as e: | |
| print(f"Streaming error: {e}") | |
| updated_history[-1][1] = f"Error during response: {e}" | |
| yield "", updated_history, None, None | |
| return | |
| full_response = self.adjust_response_based_on_state(full_response) | |
| self.update_goals(message) | |
| # Update internal state based on user input (more nuanced) | |
| emotion_deltas = {} | |
| cognitive_load_deltas = {} | |
| engagement_delta = 0 | |
| if any(word in message.lower() for word in ["sad", "unhappy", "depressed", "down"]): | |
| emotion_deltas.update({"valence": -0.2, "arousal": 0.1, "confidence": -0.1}) | |
| engagement_delta = -0.1 | |
| elif any(word in message.lower() for word in ["happy", "good", "great", "excited", "amazing"]): | |
| emotion_deltas.update({"valence": 0.2, "arousal": 0.2, "confidence": 0.1}) | |
| engagement_delta = 0.2 | |
| elif any(word in message.lower() for word in ["angry", "mad", "furious", "frustrated"]): | |
| emotion_deltas.update({"valence": -0.3, "arousal": 0.3, "dominance": -0.2, "frustration": 0.2}) | |
| engagement_delta = -0.2 | |
| elif any(word in message.lower() for word in ["scared", "afraid", "fearful", "anxious"]): | |
| emotion_deltas.update({"valence": -0.2, "arousal": 0.4, "dominance": -0.3, "confidence": -0.2}) | |
| engagement_delta = -0.1 | |
| elif any(word in message.lower() for word in ["surprise", "amazed", "astonished"]): | |
| emotion_deltas.update({"valence": 0.1, "arousal": 0.5, "dominance": 0.1, "curiosity": 0.3}) | |
| engagement_delta = 0.3 | |
| elif any(word in message.lower() for word in ["confused", "uncertain", "unsure"]): | |
| cognitive_load_deltas.update({"processing_intensity": 0.2}) | |
| emotion_deltas.update({"curiosity": 0.2, "confidence": -0.1}) | |
| engagement_delta = 0.1 | |
| else: | |
| emotion_deltas.update({"valence": 0.05, "arousal": 0.05}) | |
| engagement_delta = 0.05 | |
| if "learn" in message.lower() or "explain" in message.lower() or "know more" in message.lower(): | |
| emotion_deltas.update({"curiosity": 0.3}) | |
| cognitive_load_deltas.update({"processing_intensity": 0.1}) | |
| engagement_delta = 0.2 | |
| self.update_internal_state(emotion_deltas, cognitive_load_deltas, 0.1, engagement_delta) | |
| self.conversation_history.append(ChatMessage(role="user", content=message).to_dict()) | |
| self.conversation_history.append(ChatMessage(role="assistant", content=full_response).to_dict()) | |
| if len(self.conversation_history) > 10: | |
| self.conversation_history = self.conversation_history[-10:] | |
| custom_css = """ | |
| @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap'); | |
| body, .gradio-container { | |
| font-family: 'Inter', sans-serif !important; | |
| } | |
| .chatbot-container .message { | |
| font-family: 'Inter', sans-serif !important; | |
| } | |
| .gradio-container input, | |
| .gradio-container textarea, | |
| .gradio-container button { | |
| font-family: 'Inter', sans-serif !important; | |
| } | |
| /* Image Upload Styling */ | |
| .image-container { | |
| display: flex; | |
| gap: 10px; | |
| margin-bottom: 10px; | |
| } | |
| .image-upload { | |
| border: 1px solid #ccc; | |
| border-radius: 8px; | |
| padding: 10px; | |
| background-color: #f8f8f8; | |
| } | |
| .image-preview { | |
| max-width: 200px; | |
| max-height: 200px; | |
| border-radius: 8px; | |
| } | |
| /* Remove clear image buttons */ | |
| .clear-button { | |
| display: none; | |
| } | |
| /* Animate chatbot messages */ | |
| .chatbot-container .message { | |
| opacity: 0; | |
| animation: fadeIn 0.5s ease-in-out forwards; | |
| } | |
| @keyframes fadeIn { | |
| from { | |
| opacity: 0; | |
| transform: translateY(20px); | |
| } | |
| to { | |
| opacity: 1; | |
| transform: translateY(0); | |
| } | |
| } | |
| /* Accordion Styling and Animation */ | |
| .gr-accordion-button { | |
| background-color: #f0f0f0 !important; | |
| border-radius: 8px !important; | |
| padding: 10px !important; | |
| margin-bottom: 10px !important; | |
| transition: all 0.3s ease !important; | |
| cursor: pointer !important; | |
| } | |
| .gr-accordion-button:hover { | |
| background-color: #e0e0e0 !important; | |
| box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1) !important; | |
| } | |
| .gr-accordion-active .gr-accordion-button { | |
| background-color: #d0d0d0 !important; | |
| box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1) !important; | |
| } | |
| .gr-accordion-content { | |
| transition: max-height 0.3s ease-in-out !important; | |
| overflow: hidden !important; | |
| max-height: 0 !important; | |
| } | |
| .gr-accordion-active .gr-accordion-content { | |
| max-height: 500px !important; /* Adjust as needed */ | |
| } | |
| /* Accordion Animation - Upwards */ | |
| .gr-accordion { | |
| display: flex; | |
| flex-direction: column-reverse; | |
| } | |
| """ | |
| with gr.Blocks(theme='soft', css=custom_css) as demo: | |
| with gr.Column(): | |
| chatbot = gr.Chatbot( | |
| label="Xylaria 1.5 Senoa", | |
| height=500, | |
| show_copy_button=True, | |
| ) | |
| with gr.Accordion("Image Input", open=False, elem_classes="gr-accordion"): | |
| with gr.Row(elem_classes="image-container"): | |
| with gr.Column(elem_classes="image-upload"): | |
| img = gr.Image( | |
| sources=["upload", "webcam"], | |
| type="filepath", | |
| label="Upload Image", | |
| elem_classes="image-preview" | |
| ) | |
| with gr.Column(elem_classes="image-upload"): | |
| math_ocr_img = gr.Image( | |
| sources=["upload", "webcam"], | |
| type="filepath", | |
| label="Upload Image for Math OCR", | |
| elem_classes="image-preview" | |
| ) | |
| with gr.Row(): | |
| with gr.Column(scale=4): | |
| txt = gr.Textbox( | |
| show_label=False, | |
| placeholder="Type your message...", | |
| container=False | |
| ) | |
| btn = gr.Button("Send", scale=1) | |
| with gr.Row(): | |
| clear = gr.Button("Clear Conversation") | |
| clear_memory = gr.Button("Clear Memory") | |
| btn.click( | |
| fn=streaming_response, | |
| inputs=[txt, chatbot, img, math_ocr_img], | |
| outputs=[txt, chatbot, img, math_ocr_img] | |
| ) | |
| txt.submit( | |
| fn=streaming_response, | |
| inputs=[txt, chatbot, img, math_ocr_img], | |
| outputs=[txt, chatbot, img, math_ocr_img] | |
| ) | |
| clear.click( | |
| fn=lambda: None, | |
| inputs=None, | |
| outputs=[chatbot], | |
| queue=False | |
| ) | |
| clear_memory.click( | |
| fn=self.reset_conversation, | |
| inputs=None, | |
| outputs=[chatbot], | |
| queue=False | |
| ) | |
| demo.load(self.reset_conversation, None, None) | |
| return demo | |
| def main(): | |
| chat = XylariaChat() | |
| interface = chat.create_interface() | |
| interface.launch( | |
| share=True, | |
| debug=True | |
| ) | |
| if __name__ == "__main__": | |
| main() |