Transformers
Italian
English
semantic-search
explainable-ai
faiss
ai-ethics
responsible-ai
llm
prompt-engineering
multimodal-ai
ai-transparency
ethical-intelligence
explainable-llm
cognitive-ai
ethical-ai
scientific-retrieval
modular-ai
memory-augmented-llm
trustworthy-ai
reasoning-engine
ai-alignment
next-gen-llm
thinking-machines
open-source-ai
explainability
ai-research
semantic audit
cognitive agent
human-centered-ai
Create personalized_interaction.py
Browse files
src/interaction/personalized_interaction.py
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# © 2025 Elena Marziali — Code released under Apache 2.0 license.
|
| 2 |
+
# See LICENSE in the repository for details.
|
| 3 |
+
# Removal of this copyright is prohibited.
|
| 4 |
+
|
| 5 |
+
# This cell analyzes the user's question and adapts the response
|
| 6 |
+
# based on subject, skill level, language, and preferences.
|
| 7 |
+
|
| 8 |
+
# Analyze the question to extract intent and context
|
| 9 |
+
def analyze_question(question):
|
| 10 |
+
question_lower = question.lower()
|
| 11 |
+
if re.search(r"\d+|equation|formula|calculate|solve", question_lower):
|
| 12 |
+
return "mathematical problem"
|
| 13 |
+
elif re.search(r"anatomy|biology|description|organ|function|system", question_lower):
|
| 14 |
+
return "descriptive-biological problem"
|
| 15 |
+
elif re.search(r"experiment|measurement|test|observation", question_lower):
|
| 16 |
+
return "experimental problem"
|
| 17 |
+
else:
|
| 18 |
+
return "theoretical problem"
|
| 19 |
+
|
| 20 |
+
# Extract semantic and conceptual characteristics
|
| 21 |
+
def extract_features(problem):
|
| 22 |
+
problem_lower = problem.lower()
|
| 23 |
+
if re.search(r"\d+|equation|formula|energy|speed", problem_lower):
|
| 24 |
+
return "Chart"
|
| 25 |
+
elif re.search(r"principle|theory|model|experiment", problem_lower):
|
| 26 |
+
return "Conceptual diagram"
|
| 27 |
+
elif re.search(r"pressure|volume|temperature|transformation", problem_lower):
|
| 28 |
+
return "State diagram"
|
| 29 |
+
else:
|
| 30 |
+
return "Plain text"
|
| 31 |
+
|
| 32 |
+
# Reformulate the question to make it clearer for the model
|
| 33 |
+
def reformulate_question(question):
|
| 34 |
+
prompt = f"""Reformulate this question in a technical and precise way for a scientific AI assistant.
|
| 35 |
+
|
| 36 |
+
Question: "{question}"
|
| 37 |
+
|
| 38 |
+
Return only the reformulated question, without explanations."""
|
| 39 |
+
response = generate_response(prompt, temperature=0.5).strip()
|
| 40 |
+
|
| 41 |
+
for prefix in [
|
| 42 |
+
"The generated response to the question",
|
| 43 |
+
"Return only the reformulated question",
|
| 44 |
+
"Question:"
|
| 45 |
+
]:
|
| 46 |
+
if response.lower().startswith(prefix.lower()):
|
| 47 |
+
response = response[len(prefix):].strip(": .\"'\n")
|
| 48 |
+
|
| 49 |
+
if "\n" in response:
|
| 50 |
+
response = response.split("\n")[0].strip()
|
| 51 |
+
|
| 52 |
+
return response
|
| 53 |
+
|
| 54 |
+
# === File upload ===
|
| 55 |
+
try:
|
| 56 |
+
uploaded = files.upload()
|
| 57 |
+
file_name = list(uploaded.keys())[0]
|
| 58 |
+
file_text = extract_text(file_name)
|
| 59 |
+
|
| 60 |
+
if not file_text or file_text == "Empty or non-textual file.":
|
| 61 |
+
raise ValueError("The uploaded file does not contain valid text.")
|
| 62 |
+
except Exception as e:
|
| 63 |
+
logging.error(f"File upload error: {e}")
|
| 64 |
+
file_text = input("Manually enter the problem: ").strip()
|
| 65 |
+
|
| 66 |
+
# Save
|
| 67 |
+
with open(INDEX_FILE, "wb") as f:
|
| 68 |
+
pickle.dump(index, f)
|
| 69 |
+
|
| 70 |
+
# Load
|
| 71 |
+
with open(INDEX_FILE, "rb") as f:
|
| 72 |
+
index = pickle.load(f)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# Generate intelligent report
|
| 76 |
+
async def example_search():
|
| 77 |
+
query = "quantum physics"
|
| 78 |
+
articles = await search_multi_database(query)
|
| 79 |
+
print(articles)
|
| 80 |
+
|
| 81 |
+
# Execute the function directly with await
|
| 82 |
+
await example_search()
|
| 83 |
+
|
| 84 |
+
# === User input ===
|
| 85 |
+
import asyncio
|
| 86 |
+
|
| 87 |
+
# Validate that input is correct and coherent
|
| 88 |
+
async def get_valid_input(message, valid_options=None):
|
| 89 |
+
""" Asynchronous function to handle validated input. """
|
| 90 |
+
while True:
|
| 91 |
+
value = await asyncio.to_thread(input, message.strip())
|
| 92 |
+
value = value.strip()
|
| 93 |
+
|
| 94 |
+
if not value:
|
| 95 |
+
print("Error! Please enter a valid value.")
|
| 96 |
+
elif valid_options and value.lower() not in valid_options:
|
| 97 |
+
print(f"Error! You must choose from: {', '.join(valid_options)}")
|
| 98 |
+
else:
|
| 99 |
+
return value
|
| 100 |
+
|
| 101 |
+
example_problem = ""
|
| 102 |
+
|
| 103 |
+
while not example_problem:
|
| 104 |
+
example_problem = file_text.strip() if file_text.strip() else await get_valid_input("Enter the problem manually:")
|
| 105 |
+
|
| 106 |
+
subject = input("Enter the subject (e.g., physics, biology, etc.): ").strip().lower() or "general subject"
|
| 107 |
+
level = input("Choose the level (basic/advanced/expert): ").strip().lower()
|
| 108 |
+
while level not in ["basic", "advanced", "expert"]:
|
| 109 |
+
level = input("Error! Enter basic/advanced/expert: ").strip().lower()
|
| 110 |
+
|
| 111 |
+
topic = input("Enter the scientific problem or topic: ").strip()
|
| 112 |
+
|
| 113 |
+
chart_choice = input("Do you want a chart for the explanation? (yes/no): ").strip().lower()
|
| 114 |
+
while chart_choice not in ["yes", "no"]:
|
| 115 |
+
chart_choice = input("Error! Please answer 'yes' or 'no': ").strip().lower()
|
| 116 |
+
|
| 117 |
+
chart_requested = chart_choice == "yes"
|
| 118 |
+
|
| 119 |
+
fig = None
|
| 120 |
+
caption = ""
|
| 121 |
+
|
| 122 |
+
if chart_requested:
|
| 123 |
+
try:
|
| 124 |
+
fig, caption = generate_interactive_chart(example_problem)
|
| 125 |
+
fig.show()
|
| 126 |
+
logging.info("Chart successfully generated.")
|
| 127 |
+
except Exception as e:
|
| 128 |
+
logging.error(f"Chart generation error: {e}")
|
| 129 |
+
fig = None
|
| 130 |
+
else:
|
| 131 |
+
logging.info("Chart not requested by the user.")
|
| 132 |
+
|
| 133 |
+
available_languages = ["en", "fr", "de", "es", "zh", "ja", "ar", "it"]
|
| 134 |
+
|
| 135 |
+
target_language = input("Which language do you want the translation in? (" + ", ".join(available_languages) + "): ").strip().lower()
|
| 136 |
+
while target_language not in available_languages:
|
| 137 |
+
target_language = input("Error! Choose a valid language from: " + ", ".join(available_languages) + ": ").strip().lower()
|
| 138 |
+
|
| 139 |
+
#Secure Translation and Protected Embedding Storage
|
| 140 |
+
save_multilingual_journal(
|
| 141 |
+
journal_text=example_problem,
|
| 142 |
+
journal_id=0,
|
| 143 |
+
target_language=target_language
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
#Secure Translation and Protected Embedding Retrieval
|
| 147 |
+
similar_entries = search_similar_journals(
|
| 148 |
+
query=example_problem,
|
| 149 |
+
target_language=target_language
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
for s in similar_entries:
|
| 153 |
+
print("Similar journal:", s)
|