Browse Source

Merge 50fa71e864 into b8b2f766e5

pull/300/merge
Haq Nawaz 3 weeks ago committed by GitHub
parent
commit
d3348f8f0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1165
      week1/day1.ipynb
  2. 215
      week1/day2 EXERCISE.ipynb
  3. 434
      week1/day5.ipynb
  4. 117
      week1/week1 EXERCISE.ipynb

1165
week1/day1.ipynb

File diff suppressed because one or more lines are too long

215
week1/day2 EXERCISE.ipynb

@ -68,7 +68,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd", "id": "4e2a9393-7767-488e-a8bf-27c12dca35bd",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -82,7 +82,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"id": "29ddd15d-a3c5-4f4e-a678-873f56162724", "id": "29ddd15d-a3c5-4f4e-a678-873f56162724",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -96,7 +96,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"id": "dac0a679-599c-441f-9bf2-ddc73d35b940", "id": "dac0a679-599c-441f-9bf2-ddc73d35b940",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -110,7 +110,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"id": "7bb9c624-14f0-4945-a719-8ddb64f66f47", "id": "7bb9c624-14f0-4945-a719-8ddb64f66f47",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -124,10 +124,46 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 15,
"id": "4f2ae8e2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"NAME ID SIZE MODIFIED \n",
"llama3.2:latest a80c4f17acd5 2.0 GB 3 minutes ago \n"
]
}
],
"source": [
"!ollama list\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "479ff514-e8bd-4985-a572-2ea28bb4fa40", "id": "479ff514-e8bd-4985-a572-2ea28bb4fa40",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠋ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠙ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠸ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠼ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠴ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠦ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠧ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠇ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠏ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠋ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠙ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠹ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠸ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠼ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠴ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠦ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠧ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠇ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠏ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest ⠋ \u001b[K\u001b[?25h\u001b[?2026l\u001b[?2026h\u001b[?25l\u001b[1Gpulling manifest \u001b[K\n",
"pulling dde5aa3fc5ff... 100% ▕████████████████▏ 2.0 GB \u001b[K\n",
"pulling 966de95ca8a6... 100% ▕████████████████▏ 1.4 KB \u001b[K\n",
"pulling fcc5a6bec9da... 100% ▕████████████████▏ 7.7 KB \u001b[K\n",
"pulling a70ff7e570d9... 100% ▕████████████████▏ 6.0 KB \u001b[K\n",
"pulling 56bb8bd477a5... 100% ▕████████████████▏ 96 B \u001b[K\n",
"pulling 34bb5ab01051... 100% ▕████████████████▏ 561 B \u001b[K\n",
"verifying sha256 digest \u001b[K\n",
"writing manifest \u001b[K\n",
"success \u001b[K\u001b[?25h\u001b[?2026l\n"
]
}
],
"source": [ "source": [
"# Let's just make sure the model is loaded\n", "# Let's just make sure the model is loaded\n",
"\n", "\n",
@ -136,10 +172,38 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 19,
"id": "42b9f644-522d-4e05-a691-56e7658c0ea9", "id": "42b9f644-522d-4e05-a691-56e7658c0ea9",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generative AI has numerous business applications across various industries. Here are some examples:\n",
"\n",
"1. **Content Creation**: Generative AI can generate high-quality content such as articles, blog posts, social media posts, and even entire books. This technology is particularly useful for businesses that need to create a large amount of content quickly, but may not have the resources or expertise in-house.\n",
"2. **Visual Content Generation**: Generative AI can create visual content like images, videos, and 3D models. This technology is commonly used in advertising, marketing, and e-commerce to create visually appealing products, product demos, and social media campaigns.\n",
"3. **Chatbots and Virtual Assistants**: Generative AI can power chatbots and virtual assistants that provide customer support, answer frequently asked questions, and engage with customers in a more human-like way.\n",
"4. **Data Analysis and Insights**: Generative AI can analyze large datasets and generate insights, predictions, and recommendations. This technology is commonly used in data science, marketing analytics, and business intelligence to help organizations make data-driven decisions.\n",
"5. **Product Design and Development**: Generative AI can assist designers and engineers in product development by generating ideas, designs, and prototypes quickly and efficiently.\n",
"6. **Marketing Automation**: Generative AI can automate marketing campaigns by generating personalized content, emails, and social media posts based on customer behavior and preferences.\n",
"7. **Personalization**: Generative AI can help businesses personalize their products and services to individual customers based on their behavior, preferences, and demographics.\n",
"8. **Cybersecurity**: Generative AI can be used to detect and respond to cyber threats by analyzing network traffic, identifying patterns, and predicting potential attacks.\n",
"9. **Predictive Maintenance**: Generative AI can analyze equipment data and predict when maintenance is required, reducing downtime and increasing productivity.\n",
"10. **Autonomous Systems**: Generative AI can enable the development of autonomous systems that can operate independently, making decisions based on real-time data and feedback.\n",
"\n",
"Some specific industries that are already using generative AI include:\n",
"\n",
"1. **Retail**: Companies like Walmart and Amazon use generative AI to personalize customer experiences, optimize supply chains, and automate marketing campaigns.\n",
"2. **Finance**: Financial institutions like JPMorgan Chase and Goldman Sachs use generative AI to analyze financial data, predict market trends, and identify investment opportunities.\n",
"3. **Healthcare**: Medical organizations like the Mayo Clinic and Johnson & Johnson use generative AI to analyze medical images, diagnose diseases, and develop personalized treatment plans.\n",
"4. **Manufacturing**: Companies like Siemens and GE Aviation use generative AI to optimize production processes, design new products, and predict maintenance needs.\n",
"\n",
"These are just a few examples of the many business applications of Generative AI. As the technology continues to evolve, we can expect to see even more innovative uses across various industries.\n"
]
}
],
"source": [ "source": [
"# If this doesn't work for any reason, try the 2 versions in the following cells\n", "# If this doesn't work for any reason, try the 2 versions in the following cells\n",
"# And double check the instructions in the 'Recap on installation of Ollama' at the top of this lab\n", "# And double check the instructions in the 'Recap on installation of Ollama' at the top of this lab\n",
@ -163,10 +227,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 20,
"id": "7745b9c4-57dc-4867-9180-61fa5db55eb8", "id": "7745b9c4-57dc-4867-9180-61fa5db55eb8",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'ollama'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[20], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21;01mollama\u001b[39;00m\n\u001b[0;32m 3\u001b[0m response \u001b[38;5;241m=\u001b[39m ollama\u001b[38;5;241m.\u001b[39mchat(model\u001b[38;5;241m=\u001b[39mMODEL, messages\u001b[38;5;241m=\u001b[39mmessages)\n\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(response[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmessage\u001b[39m\u001b[38;5;124m'\u001b[39m][\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mcontent\u001b[39m\u001b[38;5;124m'\u001b[39m])\n",
"\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'ollama'"
]
}
],
"source": [ "source": [
"import ollama\n", "import ollama\n",
"\n", "\n",
@ -184,10 +260,40 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 17,
"id": "23057e00-b6fc-4678-93a9-6b31cb704bff", "id": "23057e00-b6fc-4678-93a9-6b31cb704bff",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generative AI (GeAI) has numerous business applications across various industries. Here are some examples:\n",
"\n",
"1. **Content Generation**: GeAI can create high-quality content such as articles, social media posts, product descriptions, and even entire books. This technology is being used by companies to automate the creation of content, reducing the need for human writers and improving efficiency.\n",
"2. **Virtual Assistants**: GeAI-powered virtual assistants can be used to build conversational interfaces for customer service, sales, and other applications. These AI-powered chatbots can provide personalized support, recommend products, and help customers with queries.\n",
"3. **Marketing Automation**: GeAI can generate targeted marketing campaigns using real-time data analytics, social media insights, and behavioral patterns. This technology helps businesses personalize their marketing messages, improving engagement rates and conversion rates.\n",
"4. **Product Design and Development**: GeAI-powered design tools can create product prototypes, suggest design variations, and even automate product prototyping using AI-generated 3D models.\n",
"5. **Image Recognition and Classification**: GeAI can analyze large volumes of images and identify patterns, objects, and people. This technology is being used in applications such as facial recognition, object detection, and medical image analysis.\n",
"6. **Predictive Maintenance**: GeAI-powered predictive maintenance systems can predict equipment failures, detect anomalies in network traffic, and even recommend proactive maintenance schedules.\n",
"7. **Customer Service Chatbots**: GeAI-powered chatbots can provide 24/7 customer support, answering common queries, resolving basic issues, and routing complex problems to human support agents.\n",
"8. **Creative Writing Assistance**: GeAI-powered writing tools can assist writers with content ideas, suggestions for rewording or paraphrasing text, and optimizing the style of their work.\n",
"9. **Business Strategy Development**: GeAI can analyze large datasets, identify industry trends, and provide recommendations for business strategy development.\n",
"10. **Speech Synthesis and Voice Assistants**: GeAI-powered speech synthesis systems can generate natural-sounding voices, automating tasks such as text-to-speech and voice-based customer support.\n",
"\n",
"These are just a few examples of the many potential applications of Generative AI in various industries. As the technology advances, we can expect to see even more innovative uses of GeAI across businesses and organizations.\n",
"\n",
"In addition to these specific business applications, Generative AI also poses potential risks and challenges, including:\n",
"\n",
"* Job displacement\n",
"* Data quality issues\n",
"* Bias in decision-making systems\n",
"* Dependence on AI for critical tasks\n",
"\n",
"As with any emerging technology, it is essential for businesses to carefully consider the benefits and drawbacks of using GeAI and to develop responsible guidelines and regulations for its use.\n"
]
}
],
"source": [ "source": [
"# There's actually an alternative approach that some people might prefer\n", "# There's actually an alternative approach that some people might prefer\n",
"# You can use the OpenAI client python library to call Ollama:\n", "# You can use the OpenAI client python library to call Ollama:\n",
@ -285,16 +391,95 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 23,
"id": "6de38216-6d1c-48c4-877b-86d403f4e0f8", "id": "6de38216-6d1c-48c4-877b-86d403f4e0f8",
"metadata": {}, "metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"This is a basic HTML template for a website. Here's a summary of the key elements:\n",
"\n",
"1. **Head Section**:\n",
"\t* Specifies the character encoding (UTF-8) and viewport settings.\n",
"\t* Links to external resources, including:\n",
"\t\t+ Font Awesome CDN for icons\n",
"\t\t+ Google Fonts for typography (Inter)\n",
"\t\t+ A favicon image\n",
"\t* References an internal stylesheet (`style.css`) for CSS layout.\n",
"\n",
"2. **Title**:\n",
"\t* Sets the title of the webpage (\"Haq Nawaz - Bridging AI & Islamic Scholarship\").\n",
"\n",
"3. **JavaScript Code**:\n",
"\t* Handles menu toggling on mobile devices.\n",
"\t+ Adds a click event listener to toggle the `.nav-links` class and update the hamburger icon's text.\n",
"\t+ Closes the menu when clicking outside the `nav` element or a dropdown.\n",
"\n",
"4. **Layout**:\n",
"\t* The code snippets above suggest that the webpage uses a basic layout with a hamburger menu, which is toggled using JavaScript.\n",
"\n",
"However, without more information about the complete HTML structure and code, it's difficult to provide a detailed explanation of how this template interacts with other pages or external resources. \n",
"\n",
"There are several potential security issues:\n",
"\n",
"* The link integrity is specified incorrectly with `...your-integrity-key...\",` should be replaced by a valid hash for Font Awesome.\n",
"* The `crossorigin=\"anonymous\"`, setting prevents proper DNS lookup, and may cause issues.\n",
"\n",
"The given template seems to be missing a few sections typically found in an HTML page, such as:\n",
"\n",
"- `<body>` tag,\n",
"- Content,\n",
"- footer"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from openai import OpenAI\n",
"from IPython.display import display, Markdown\n",
"\n",
"openai = OpenAI(\n",
" base_url=\"http://localhost:11434/v1\",\n",
" api_key=\"ollama\"\n",
")\n",
"\n",
"def messages_for(content):\n",
" return [{\"role\": \"user\", \"content\": f\"Summarize this website:\\n{content}\"}]\n",
"\n",
"def summarize(url):\n",
" import requests\n",
" website = requests.get(url).text[:2000] # Keep it short for local model\n",
" response = openai.chat.completions.create(\n",
" model=\"llama3.2\",\n",
" messages=messages_for(website)\n",
" )\n",
" return response.choices[0].message.content\n",
"\n",
"def display_summary(url):\n",
" summary = summarize(url)\n",
" display(Markdown(summary))\n",
"\n",
"display_summary(\"https://haqnawaz.org\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29563f16",
"metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": []
} }
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3 (ipykernel)", "display_name": "venv",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
}, },
@ -308,7 +493,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.11" "version": "3.10.0rc2"
} }
}, },
"nbformat": 4, "nbformat": 4,

434
week1/day5.ipynb

@ -22,7 +22,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"id": "d5b08506-dc8b-4443-9201-5f1848161363", "id": "d5b08506-dc8b-4443-9201-5f1848161363",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -42,10 +42,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"id": "fc5d8880-f2ee-4c06-af16-ecbc0262af61", "id": "fc5d8880-f2ee-4c06-af16-ecbc0262af61",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"API key looks good so far\n"
]
}
],
"source": [ "source": [
"# Initialize and constants\n", "# Initialize and constants\n",
"\n", "\n",
@ -63,7 +71,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 3,
"id": "106dd65e-90af-4ca8-86b6-23a41840645b", "id": "106dd65e-90af-4ca8-86b6-23a41840645b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -101,12 +109,75 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"id": "e30d8128-933b-44cc-81c8-ab4c9d86589a", "id": "e30d8128-933b-44cc-81c8-ab4c9d86589a",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"['#introduction',\n",
" '#imam-budair',\n",
" '#imam-juhany',\n",
" '#imam-ghamidi',\n",
" '#isdp',\n",
" '#altasreef',\n",
" '#easyquran',\n",
" '#mohaddis',\n",
" '#tanzeem',\n",
" '#ai-nlp-expertise',\n",
" '#The-Noble-Quran',\n",
" '#Tazkeer-ul-Quran',\n",
" '#seerat-quiz',\n",
" '#tuheed',\n",
" '#publications',\n",
" '#brainiac',\n",
" '#nascon',\n",
" '#contact',\n",
" '#introduction',\n",
" '#contact',\n",
" 'https://jamiaashrafia.org/',\n",
" 'https://ili.digital/',\n",
" 'https://www.easyquranwahadees.com/',\n",
" 'https://tanzeemdigitallibrary.com/',\n",
" 'https://altasreef.vercel.app',\n",
" 'https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran',\n",
" 'https://play.google.com/store/apps/details?id=com.asantarjuma',\n",
" 'https://play.google.com/store/apps/details?id=tazkeerul.quran',\n",
" 'https://play.google.com/store/apps/details?id=com.anfzquizapp',\n",
" 'https://play.google.com/store/apps/details?id=com.tuheed',\n",
" 'https://isdp.info',\n",
" 'https://isdp.info/students-in-field.php',\n",
" 'https://altasreef.vercel.app',\n",
" 'https://altasreef.vercel.app/exam',\n",
" 'https://aclanthology.org/2025.abjadnlp-1.14.pdf',\n",
" 'https://www.easyquranwahadees.com/',\n",
" 'https://mohaddis.com/',\n",
" 'https://tanzeemdigitallibrary.com/',\n",
" 'https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran',\n",
" 'https://play.google.com/store/apps/details?id=tazkeerul.quran',\n",
" 'https://play.google.com/store/apps/details?id=com.anfzquizapp',\n",
" 'https://play.google.com/store/apps/details?id=com.tuheed',\n",
" 'https://aclanthology.org/2025.abjadnlp-1.14.pdf',\n",
" 'https://link.springer.com/article/10.1007/s10462-022-10313-2',\n",
" 'https://aclanthology.org/2022.osact-1.18/',\n",
" 'https://link.springer.com/chapter/10.1007/978-3-030-22871-2_35',\n",
" 'https://ejournal.um.edu.my/index.php/quranica/article/view/12108',\n",
" 'https://ieeexplore.ieee.org/document/7277250',\n",
" 'https://scholar.google.com/citations?user=AulVnWcAAAAJ&hl=en',\n",
" 'https://altasreef.vercel.app',\n",
" 'mailto:haq@haqnawaz.org',\n",
" 'https://github.com/haqnawaz99',\n",
" 'https://linkedin.com/in/haqnawaz99']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"ed = Website(\"https://edwarddonner.com\")\n", "ed = Website(\"https://haqnawaz.org\")\n",
"ed.links" "ed.links"
] ]
}, },
@ -128,7 +199,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"id": "6957b079-0d96-45f7-a26a-3487510e9b35", "id": "6957b079-0d96-45f7-a26a-3487510e9b35",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -149,17 +220,63 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 6,
"id": "b97e4068-97ed-4120-beae-c42105e4d59a", "id": "a7f284d7",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [
"link_system_prompt = (\n",
" \"You are provided with a list of links found on a personal portfolio website. \"\n",
" \"Your task is to identify the links most relevant for inclusion in a personal profile brochure, \"\n",
" \"such as links to About, Projects, AI & NLP, Mobile Apps, Publications, Awards, or Contact sections.\\n\"\n",
" \"Respond in JSON format as shown below:\\n\"\n",
" \"{\\n\"\n",
" ' \"links\": [\\n'\n",
" ' {\"type\": \"about section\", \"url\": \"https://haqnawaz.org#about-me\"},\\n'\n",
" ' {\"type\": \"projects section\", \"url\": \"https://haqnawaz.org#projects\"},\\n'\n",
" ' {\"type\": \"AI & NLP section\", \"url\": \"https://haqnawaz.org#ai-nlp\"},\\n'\n",
" ' {\"type\": \"mobile apps section\", \"url\": \"https://haqnawaz.org#mobile-apps\"},\\n'\n",
" ' {\"type\": \"publications section\", \"url\": \"https://haqnawaz.org#publications\"},\\n'\n",
" ' {\"type\": \"awards section\", \"url\": \"https://haqnawaz.org#awards\"},\\n'\n",
" ' {\"type\": \"contact section\", \"url\": \"https://haqnawaz.org#contact\"}\\n'\n",
" ' ]\\n'\n",
" \"}\"\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b97e4068-97ed-4120-beae-c42105e4d59a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You are provided with a list of links found on a personal portfolio website. Your task is to identify the links most relevant for inclusion in a personal profile brochure, such as links to About, Projects, AI & NLP, Mobile Apps, Publications, Awards, or Contact sections.\n",
"Respond in JSON format as shown below:\n",
"{\n",
" \"links\": [\n",
" {\"type\": \"about section\", \"url\": \"https://haqnawaz.org#about-me\"},\n",
" {\"type\": \"projects section\", \"url\": \"https://haqnawaz.org#projects\"},\n",
" {\"type\": \"AI & NLP section\", \"url\": \"https://haqnawaz.org#ai-nlp\"},\n",
" {\"type\": \"mobile apps section\", \"url\": \"https://haqnawaz.org#mobile-apps\"},\n",
" {\"type\": \"publications section\", \"url\": \"https://haqnawaz.org#publications\"},\n",
" {\"type\": \"awards section\", \"url\": \"https://haqnawaz.org#awards\"},\n",
" {\"type\": \"contact section\", \"url\": \"https://haqnawaz.org#contact\"}\n",
" ]\n",
"}\n"
]
}
],
"source": [ "source": [
"print(link_system_prompt)" "print(link_system_prompt)"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 8,
"id": "8e1f601b-2eaf-499d-b6b8-c99050c9d6b3", "id": "8e1f601b-2eaf-499d-b6b8-c99050c9d6b3",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -175,17 +292,79 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 9,
"id": "6bcbfa78-6395-4685-b92c-22d592050fd7", "id": "6bcbfa78-6395-4685-b92c-22d592050fd7",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Here is the list of links on the website of https://haqnawaz.org - please decide which of these are relevant web links for a brochure about the company, respond with the full https URL in JSON format. Do not include Terms of Service, Privacy, email links.\n",
"Links (some might be relative links):\n",
"#introduction\n",
"#imam-budair\n",
"#imam-juhany\n",
"#imam-ghamidi\n",
"#isdp\n",
"#altasreef\n",
"#easyquran\n",
"#mohaddis\n",
"#tanzeem\n",
"#ai-nlp-expertise\n",
"#The-Noble-Quran\n",
"#Tazkeer-ul-Quran\n",
"#seerat-quiz\n",
"#tuheed\n",
"#publications\n",
"#brainiac\n",
"#nascon\n",
"#contact\n",
"#introduction\n",
"#contact\n",
"https://jamiaashrafia.org/\n",
"https://ili.digital/\n",
"https://www.easyquranwahadees.com/\n",
"https://tanzeemdigitallibrary.com/\n",
"https://altasreef.vercel.app\n",
"https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran\n",
"https://play.google.com/store/apps/details?id=com.asantarjuma\n",
"https://play.google.com/store/apps/details?id=tazkeerul.quran\n",
"https://play.google.com/store/apps/details?id=com.anfzquizapp\n",
"https://play.google.com/store/apps/details?id=com.tuheed\n",
"https://isdp.info\n",
"https://isdp.info/students-in-field.php\n",
"https://altasreef.vercel.app\n",
"https://altasreef.vercel.app/exam\n",
"https://aclanthology.org/2025.abjadnlp-1.14.pdf\n",
"https://www.easyquranwahadees.com/\n",
"https://mohaddis.com/\n",
"https://tanzeemdigitallibrary.com/\n",
"https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran\n",
"https://play.google.com/store/apps/details?id=tazkeerul.quran\n",
"https://play.google.com/store/apps/details?id=com.anfzquizapp\n",
"https://play.google.com/store/apps/details?id=com.tuheed\n",
"https://aclanthology.org/2025.abjadnlp-1.14.pdf\n",
"https://link.springer.com/article/10.1007/s10462-022-10313-2\n",
"https://aclanthology.org/2022.osact-1.18/\n",
"https://link.springer.com/chapter/10.1007/978-3-030-22871-2_35\n",
"https://ejournal.um.edu.my/index.php/quranica/article/view/12108\n",
"https://ieeexplore.ieee.org/document/7277250\n",
"https://scholar.google.com/citations?user=AulVnWcAAAAJ&hl=en\n",
"https://altasreef.vercel.app\n",
"mailto:haq@haqnawaz.org\n",
"https://github.com/haqnawaz99\n",
"https://linkedin.com/in/haqnawaz99\n"
]
}
],
"source": [ "source": [
"print(get_links_user_prompt(ed))" "print(get_links_user_prompt(ed))"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 10,
"id": "a29aca19-ca13-471c-a4b4-5abbfa813f69", "id": "a29aca19-ca13-471c-a4b4-5abbfa813f69",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -206,10 +385,102 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 11,
"id": "74a827a0-2782-4ae5-b210-4a242a8b4cc2", "id": "74a827a0-2782-4ae5-b210-4a242a8b4cc2",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"['/',\n",
" '/models',\n",
" '/datasets',\n",
" '/spaces',\n",
" '/posts',\n",
" '/docs',\n",
" '/enterprise',\n",
" '/pricing',\n",
" '/login',\n",
" '/join',\n",
" '/spaces',\n",
" '/models',\n",
" '/Qwen/Qwen2.5-Omni-7B',\n",
" '/deepseek-ai/DeepSeek-V3-0324',\n",
" '/all-hands/openhands-lm-32b-v0.1',\n",
" '/meta-llama/Llama-4-Scout-17B-16E-Instruct',\n",
" '/openfree/flux-chatgpt-ghibli-lora',\n",
" '/models',\n",
" '/spaces/enzostvs/deepsite',\n",
" '/spaces/jamesliu1217/EasyControl_Ghibli',\n",
" '/spaces/VAST-AI/TripoSG',\n",
" '/spaces/ByteDance/InfiniteYou-FLUX',\n",
" '/spaces/Stable-X/Hi3DGen',\n",
" '/spaces',\n",
" '/datasets/nvidia/Llama-Nemotron-Post-Training-Dataset-v1',\n",
" '/datasets/virtuoussy/Multi-subject-RLVR',\n",
" '/datasets/glaiveai/reasoning-v1-20m',\n",
" '/datasets/open-thoughts/OpenThoughts2-1M',\n",
" '/datasets/FreedomIntelligence/medical-o1-reasoning-SFT',\n",
" '/datasets',\n",
" '/join',\n",
" '/pricing#endpoints',\n",
" '/pricing#spaces',\n",
" '/pricing',\n",
" '/enterprise',\n",
" '/enterprise',\n",
" '/enterprise',\n",
" '/enterprise',\n",
" '/enterprise',\n",
" '/enterprise',\n",
" '/enterprise',\n",
" '/allenai',\n",
" '/facebook',\n",
" '/amazon',\n",
" '/google',\n",
" '/Intel',\n",
" '/microsoft',\n",
" '/grammarly',\n",
" '/Writer',\n",
" '/docs/transformers',\n",
" '/docs/diffusers',\n",
" '/docs/safetensors',\n",
" '/docs/huggingface_hub',\n",
" '/docs/tokenizers',\n",
" '/docs/trl',\n",
" '/docs/transformers.js',\n",
" '/docs/smolagents',\n",
" '/docs/peft',\n",
" '/docs/datasets',\n",
" '/docs/text-generation-inference',\n",
" '/docs/accelerate',\n",
" '/models',\n",
" '/datasets',\n",
" '/spaces',\n",
" '/tasks',\n",
" 'https://ui.endpoints.huggingface.co',\n",
" '/chat',\n",
" '/huggingface',\n",
" '/brand',\n",
" '/terms-of-service',\n",
" '/privacy',\n",
" 'https://apply.workable.com/huggingface/',\n",
" 'mailto:press@huggingface.co',\n",
" '/learn',\n",
" '/docs',\n",
" '/blog',\n",
" 'https://discuss.huggingface.co',\n",
" 'https://status.huggingface.co/',\n",
" 'https://github.com/huggingface',\n",
" 'https://twitter.com/huggingface',\n",
" 'https://www.linkedin.com/company/huggingface/',\n",
" '/join/discord']"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"# Anthropic has made their site harder to scrape, so I'm using HuggingFace..\n", "# Anthropic has made their site harder to scrape, so I'm using HuggingFace..\n",
"\n", "\n",
@ -219,10 +490,35 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 12,
"id": "d3d583e2-dcc4-40cc-9b28-1e8dbf402924", "id": "d3d583e2-dcc4-40cc-9b28-1e8dbf402924",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"{'links': [{'type': 'home', 'url': 'https://huggingface.co/'},\n",
" {'type': 'models section', 'url': 'https://huggingface.co/models'},\n",
" {'type': 'datasets section', 'url': 'https://huggingface.co/datasets'},\n",
" {'type': 'spaces section', 'url': 'https://huggingface.co/spaces'},\n",
" {'type': 'posts section', 'url': 'https://huggingface.co/posts'},\n",
" {'type': 'documentation', 'url': 'https://huggingface.co/docs'},\n",
" {'type': 'enterprise', 'url': 'https://huggingface.co/enterprise'},\n",
" {'type': 'pricing', 'url': 'https://huggingface.co/pricing'},\n",
" {'type': 'blog', 'url': 'https://huggingface.co/blog'},\n",
" {'type': 'community discussion', 'url': 'https://discuss.huggingface.co'},\n",
" {'type': 'status', 'url': 'https://status.huggingface.co/'},\n",
" {'type': 'GitHub', 'url': 'https://github.com/huggingface'},\n",
" {'type': 'Twitter', 'url': 'https://twitter.com/huggingface'},\n",
" {'type': 'LinkedIn',\n",
" 'url': 'https://www.linkedin.com/company/huggingface/'}]}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"get_links(\"https://huggingface.co\")" "get_links(\"https://huggingface.co\")"
] ]
@ -239,7 +535,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 13,
"id": "85a5b6e2-e7ef-44a9-bc7f-59ede71037b5", "id": "85a5b6e2-e7ef-44a9-bc7f-59ede71037b5",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -257,17 +553,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 14,
"id": "5099bd14-076d-4745-baf3-dac08d8e5ab2", "id": "5099bd14-076d-4745-baf3-dac08d8e5ab2",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"print(get_all_details(\"https://huggingface.co\"))" "# print(get_all_details(\"https://huggingface.co\"))"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 15,
"id": "9b863a55-f86c-4e3f-8a79-94e24c1a8cf2", "id": "9b863a55-f86c-4e3f-8a79-94e24c1a8cf2",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -285,7 +581,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 16,
"id": "6ab83d92-d36b-4ce0-8bcc-5bb4c2f8ff23", "id": "6ab83d92-d36b-4ce0-8bcc-5bb4c2f8ff23",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -303,14 +599,22 @@
"execution_count": null, "execution_count": null,
"id": "cd909e0b-1312-4ce2-a553-821e795d7572", "id": "cd909e0b-1312-4ce2-a553-821e795d7572",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found links: {'links': [{'type': 'homepage', 'url': 'https://huggingface.co/'}, {'type': 'models section', 'url': 'https://huggingface.co/models'}, {'type': 'datasets section', 'url': 'https://huggingface.co/datasets'}, {'type': 'spaces section', 'url': 'https://huggingface.co/spaces'}, {'type': 'posts section', 'url': 'https://huggingface.co/posts'}, {'type': 'documentation section', 'url': 'https://huggingface.co/docs'}, {'type': 'enterprise section', 'url': 'https://huggingface.co/enterprise'}, {'type': 'pricing section', 'url': 'https://huggingface.co/pricing'}, {'type': 'blog section', 'url': 'https://huggingface.co/blog'}, {'type': 'discussion forum', 'url': 'https://discuss.huggingface.co'}, {'type': 'GitHub repository', 'url': 'https://github.com/huggingface'}, {'type': 'Twitter profile', 'url': 'https://twitter.com/huggingface'}, {'type': 'LinkedIn profile', 'url': 'https://www.linkedin.com/company/huggingface/'}]}\n"
]
}
],
"source": [ "source": [
"get_brochure_user_prompt(\"HuggingFace\", \"https://huggingface.co\")" "# get_brochure_user_prompt(\"HuggingFace\", \"https://huggingface.co\")"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 17,
"id": "e44de579-4a1a-4e6a-a510-20ea3e4b8d46", "id": "e44de579-4a1a-4e6a-a510-20ea3e4b8d46",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -350,7 +654,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 18,
"id": "51db0e49-f261-4137-aabe-92dd601f7725", "id": "51db0e49-f261-4137-aabe-92dd601f7725",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -375,12 +679,78 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 20,
"id": "56bf0ae3-ee9d-4a72-9cd6-edcac67ceb6d", "id": "56bf0ae3-ee9d-4a72-9cd6-edcac67ceb6d",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found links: {'links': [{'type': 'about section', 'url': 'https://haqnawaz.org#introduction'}, {'type': 'projects section', 'url': 'https://haqnawaz.org#projects'}, {'type': 'AI & NLP section', 'url': 'https://haqnawaz.org#ai-nlp-expertise'}, {'type': 'mobile apps section', 'url': 'https://play.google.com/store/apps/details?id=com.maktaba.maariful.quran'}, {'type': 'mobile apps section', 'url': 'https://play.google.com/store/apps/details?id=tazkeerul.quran'}, {'type': 'mobile apps section', 'url': 'https://play.google.com/store/apps/details?id=com.anfzquizapp'}, {'type': 'mobile apps section', 'url': 'https://play.google.com/store/apps/details?id=com.tuheed'}, {'type': 'publications section', 'url': 'https://aclanthology.org/2025.abjadnlp-1.14.pdf'}, {'type': 'publications section', 'url': 'https://link.springer.com/article/10.1007/s10462-022-10313-2'}, {'type': 'publications section', 'url': 'https://aclanthology.org/2022.osact-1.18/'}, {'type': 'publications section', 'url': 'https://link.springer.com/chapter/10.1007/978-3-030-22871-2_35'}, {'type': 'publications section', 'url': 'https://ejournal.um.edu.my/index.php/quranica/article/view/12108'}, {'type': 'publications section', 'url': 'https://ieeexplore.ieee.org/document/7277250'}, {'type': 'awards section', 'url': 'https://github.com/haqnawaz99'}, {'type': 'contact section', 'url': 'https://haqnawaz.org#contact'}, {'type': 'contact section', 'url': 'https://linkedin.com/in/haqnawaz99'}]}\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some characters could not be decoded, and were replaced with REPLACEMENT CHARACTER.\n"
]
},
{
"data": {
"text/markdown": [
"# HuggingFace Brochure\n",
"\n",
"## Bridging AI & Islamic Scholarship\n",
"\n",
"### About Us\n",
"HuggingFace, spearheaded by *Haq Nawaz*, is at the forefront of merging modern artificial intelligence (AI) with classical Islamic scholarship. With a robust background in Natural Language Processing (NLP), Haq Nawaz is not just a faculty member at Jamia Ashrafia Lahore but is also a dedicated PhD scholar and data science consultant. Our mission is to innovate and digitize Islamic education through award-winning software and applications, enhancing accessibility and engagement with religious texts.\n",
"\n",
"### Our Vision\n",
"We are committed to pioneering efforts in Islamic software development, creating tools that benefit education, research, and broader public outreach. Our projects aim to make classical Islamic literature, the Qur'an, and Hadith easily searchable and interactable, ensuring that this vital knowledge is accessible to everyone.\n",
"\n",
"### Notable Projects\n",
"- **Easy Quran**: An interactive platform that simplifies access to Quranic texts.\n",
"- **Mohaddis**: A tailored solution for Hadith studies.\n",
"- **Tanzeem**: A digital library assembling Islamic references.\n",
"- **Altasreef**: An award-winning platform for Arabic verb conjugation that earned 1st prize at UET Lahore’s Brainiac 2018.\n",
"- **Mobile Applications**:\n",
" - **The Noble Quran**: Offering esteemed translations and tafsir.\n",
" - **Tazkeer Ul Quran**: Multilingual thematic analysis.\n",
" - **Seerat Quiz**: Educational interactive platform for children.\n",
"\n",
"### Recognition\n",
"HuggingFace's contributions have been nationally recognized at events like NaSCon 2019 for innovative research and development in Quranic NLP annotation. We pride ourselves on our role in the wider scholarly community, having hosted discussions with esteemed Islamic leaders regarding the integration of technology and traditional scholarship.\n",
"\n",
"### Company Culture\n",
"At HuggingFace, we foster an environment that encourages collaboration, innovation, and a deep respect for the principles of Islamic scholarship. Our culture thrives on bridging traditional and modern methodologies, promoting continuous learning, and empowering our employees to contribute to meaningful projects that have a tangible impact on society.\n",
"\n",
"### Career Opportunities\n",
"We are always on the lookout for passionate individuals who desire to contribute to the intersection of AI and Islamic scholarship. Our **Intensive Software Development Program (ISDP)** equips recent graduates of Islamic madrasas with essential computer science and software engineering skills, preparing them for rewarding careers in technology.\n",
"\n",
"Join us in our mission to inspire future generations by advancing Islamic knowledge through technology. Together, we can redefine how classical learning is approached in the digital age.\n",
"\n",
"## Get in Touch\n",
"For inquiries related to partnerships, careers, or projects, contact us at:\n",
"- Email: [info@huggingface.com](mailto:info@huggingface.com)\n",
"- Phone: +123-456-7890\n",
"\n",
"### Follow Us\n",
"Stay updated with our latest initiatives and innovations by following us on our social media channels! \n",
"\n",
"Together, let’s bridge the gap between AI and Islamic scholarship."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [ "source": [
"stream_brochure(\"HuggingFace\", \"https://huggingface.co\")" "stream_brochure(\"HuggingFace\", \"https://haqnawaz.org\")"
] ]
}, },
{ {
@ -487,7 +857,7 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3 (ipykernel)", "display_name": "venv",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
}, },
@ -501,7 +871,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.11" "version": "3.10.0"
} }
}, },
"nbformat": 4, "nbformat": 4,

117
week1/week1 EXERCISE.ipynb

@ -13,17 +13,26 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"id": "c1070317-3ed9-4659-abe3-828943230e03", "id": "c1070317-3ed9-4659-abe3-828943230e03",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# imports" "# imports\n",
"\n",
"import os\n",
"import requests\n",
"import json\n",
"from typing import List\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display, update_display\n",
"from openai import OpenAI"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 2,
"id": "4a456906-915a-4bfd-bb9d-57e505c5093f", "id": "4a456906-915a-4bfd-bb9d-57e505c5093f",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@ -36,12 +45,33 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 4,
"id": "a8d7923c-5f28-4c30-8556-342d7c8497c1", "id": "a8d7923c-5f28-4c30-8556-342d7c8497c1",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"API key found and looks good so far!\n"
]
}
],
"source": [ "source": [
"# set up environment" "# set up environment\n",
"load_dotenv(override=True)\n",
"api_key = os.getenv('OPENAI_API_KEY')\n",
"\n",
"# Check the key\n",
"\n",
"if not api_key:\n",
" print(\"No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!\")\n",
"elif not api_key.startswith(\"sk-proj-\"):\n",
" print(\"An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook\")\n",
"elif api_key.strip() != api_key:\n",
" print(\"An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook\")\n",
"else:\n",
" print(\"API key found and looks good so far!\")"
] ]
}, },
{ {
@ -61,12 +91,77 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 5,
"id": "60ce7000-a4a5-4cce-a261-e75ef45063b4", "id": "60ce7000-a4a5-4cce-a261-e75ef45063b4",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/markdown": [
"Certainly! This line of code is using a combination of Python set comprehension and the `yield from` statement. Let's break it down step-by-step:\n",
"\n",
"1. **Set Comprehension**: \n",
" - `{book.get(\"author\") for book in books if book.get(\"author\")}` is a set comprehension. It constructs a set containing the authors from the `books` collection.\n",
" - `books` is assumed to be an iterable, such as a list or tuple, of dictionaries, where each dictionary represents a book with various attributes, including an \"author\".\n",
" - `book.get(\"author\")` attempts to retrieve the value associated with the \"author\" key from each book dictionary.\n",
" - The `if book.get(\"author\")` part ensures that only books with a non-None value for the \"author\" key are included in the set. This helps in filtering out entries where the \"author\" key doesn't exist or is set to `None`.\n",
"\n",
"2. **Yield From**: \n",
" - `yield from` is a statement in Python that delegates part of a generator's operations to another iterable.\n",
" - `yield from` in this context is used to yield each element from the set constructed by the set comprehension one at a time.\n",
"\n",
"3. **Purpose**:\n",
" - The combination of set comprehension and `yield from` results in yielding each distinct author found in the list of books, but importantly, without duplicates, since sets naturally eliminate duplicates.\n",
" - This might be part of a generator function, which is a function allowing iteration over the unique authors.\n",
"\n",
"In summary, this code is used to retrieve and yield each unique author's name from a list of book dictionaries, one by one, eliminating any duplicates. The use of `yield from` makes it efficient for building generators that process data as an iterable sequence."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [ "source": [
"# Get gpt-4o-mini to answer, with streaming" "# Get gpt-4o-mini to answer, with streaming\n",
"from IPython.display import display, Markdown\n",
"import openai\n",
"\n",
"# Your model\n",
"MODEL = \"gpt-4o\" \n",
"# System prompt (sets the assistant's role)\n",
"system_prompt = \"You are a helpful Python expert who explains code clearly and concisely.\"\n",
"\n",
"# The user question\n",
"question = \"\"\"\n",
"Please explain what this code does and why:\n",
"yield from {book.get(\"author\") for book in books if book.get(\"author\")}\n",
"\"\"\"\n",
"\n",
"# Streaming function for explanation\n",
"def stream_code_explanation(code_question):\n",
" stream = openai.chat.completions.create(\n",
" model=MODEL,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": code_question}\n",
" ],\n",
" stream=True\n",
" )\n",
" \n",
" response = \"\"\n",
" display_handle = display(Markdown(\"\"), display_id=True)\n",
" \n",
" for chunk in stream:\n",
" content = chunk.choices[0].delta.content or \"\"\n",
" response += content\n",
" response_cleaned = response.replace(\"```\", \"\").replace(\"markdown\", \"\")\n",
" display_handle.update(Markdown(response_cleaned))\n",
"\n",
"# Run it!\n",
"stream_code_explanation(question)\n"
] ]
}, },
{ {
@ -82,7 +177,7 @@
], ],
"metadata": { "metadata": {
"kernelspec": { "kernelspec": {
"display_name": "Python 3 (ipykernel)", "display_name": "venv",
"language": "python", "language": "python",
"name": "python3" "name": "python3"
}, },
@ -96,7 +191,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.11.11" "version": "3.10.0rc2"
} }
}, },
"nbformat": 4, "nbformat": 4,

Loading…
Cancel
Save