Browse Source

unexpected merge conflict

pull/150/head
udomai 3 months ago
parent
commit
3613c4f171
  1. 256
      week1/community-contributions/day-1-ollama-app.ipynb
  2. 233
      week1/community-contributions/day1-research-paper-summarization.ipynb
  3. 224
      week1/community-contributions/day1-webscraping-playwright.ipynb
  4. 192
      week1/community-contributions/day2 EXERCISE-Summarization-Ollama.ipynb
  5. 182
      week2/community-contributions/day3-gradio-auth.ipynb
  6. 908
      week4/community-contributions/day4-gemini-included.ipynb

256
week1/community-contributions/day-1-ollama-app.ipynb

@ -0,0 +1,256 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import tkinter and ollama to create the app"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"import ollama\n",
"import tkinter as tk\n",
"from tkinter import ttk"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Basic configuration parameters for the Ollama API:"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
"HEADERS = {\"Content-Type\":\"application/json\"}\n",
"MODEL = \"llama3.2\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialize conversation history."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"conversation_history = []"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Defining the key presses. If user presses shit + enter then simply go to the next line. \n",
"\n",
"If user presses only enter then submit the question."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"def handle_keypress(event):\n",
" if event.state & 0x1: # Check if Shift is pressed\n",
" return\n",
" else:\n",
" display_answer()\n",
" return 'break'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Defining the function that will display answers using Ollama.\n",
"\n",
"\n",
"To turn it into a chatbot we simply append user's question and Ollama's response to our conversation history and pass that into Ollama as our next question."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"def display_answer(event=None):\n",
" question_text['state'] = 'disabled'\n",
" question_text['bg'] = '#F0F0F0'\n",
" status_label.config(text=\"Looking for an answer...\")\n",
" root.update()\n",
"\n",
" # Get question text and prepare message\n",
" question = question_text.get(\"1.0\", tk.END).strip()\n",
" if question:\n",
" # Append the user's question to the conversation history\n",
" conversation_history.append({\"role\": \"user\", \"content\": question})\n",
"\n",
" # Pass the entire conversation history to Ollama\n",
" try:\n",
" # Get the answer\n",
" response = ollama.chat(model=MODEL, messages=conversation_history)\n",
" answer = response[\"message\"][\"content\"]\n",
"\n",
" # Append the assistant's answer to the conversation history\n",
" conversation_history.append({\"role\": \"assistant\", \"content\": answer})\n",
"\n",
" # Update the text widget with the answer\n",
" answer_text.configure(state='normal')\n",
" answer_text.delete(1.0, tk.END)\n",
" answer_text.insert(tk.END, answer)\n",
" answer_text.configure(state='disabled')\n",
"\n",
" status_label.config(text=\"Answered\")\n",
" except Exception as e:\n",
" answer_text.configure(state='normal')\n",
" answer_text.delete(1.0, tk.END)\n",
" answer_text.insert(tk.END, f\"Error: {str(e)}\")\n",
" answer_text.configure(state='disabled')\n",
" status_label.config(text=\"Error\")\n",
" else:\n",
" # If empty question string was received\n",
" answer_text.configure(state='normal')\n",
" answer_text.delete(1.0, tk.END)\n",
" answer_text.insert(tk.END, \"Please enter a question.\")\n",
" answer_text.configure(state='disabled')\n",
" status_label.config(text=\"\")\n",
"\n",
" # Re-enable question input and restore normal background\n",
" question_text['state'] = 'normal'\n",
" question_text['bg'] = 'white'\n",
" root.update()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A button to remove the conversation history and start all over again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def remove_all():\n",
" \"\"\"Clears the conversation history and resets the interface.\"\"\"\n",
" global conversation_history\n",
" conversation_history = [] # Clear conversation history\n",
"\n",
" # Reset text widgets\n",
" question_text.delete(1.0, tk.END)\n",
" answer_text.configure(state='normal')\n",
" answer_text.delete(1.0, tk.END)\n",
" answer_text.insert(tk.END, \"Your answer will appear here.\")\n",
" answer_text.configure(state='disabled')\n",
"\n",
" # Reset status label\n",
" status_label.config(text=\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Creating the app window using tkinter."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Create the main window\n",
"root = tk.Tk()\n",
"root.title(\"Ollama with GUI\")\n",
"root.geometry(\"500x800\")\n",
"\n",
"# Create and configure the Questions window\n",
"question_frame = ttk.LabelFrame(root, text=\"Questions\", padding=(10, 10))\n",
"question_frame.pack(fill=\"both\", expand=True, padx=10, pady=10)\n",
"\n",
"question_label = ttk.Label(question_frame, text=\"Enter your question:\")\n",
"question_label.pack(anchor=\"w\", pady=5)\n",
"\n",
"# Replace Entry with Text widget for questions\n",
"question_text = tk.Text(question_frame, wrap=tk.WORD, width=50, height=4)\n",
"question_text.pack(anchor=\"w\", pady=5)\n",
"question_text.bind(\"<Return>\", handle_keypress)\n",
"\n",
"# Add status label\n",
"status_label = ttk.Label(question_frame, text=\"\")\n",
"status_label.pack(anchor=\"w\", pady=5)\n",
"\n",
"# Add Remove All button\n",
"remove_all_button = ttk.Button(question_frame, text=\"Remove All\", command=remove_all)\n",
"remove_all_button.pack(anchor=\"e\", pady=5)\n",
"\n",
"# Create and configure the Answers window\n",
"answer_frame = ttk.LabelFrame(root, text=\"Answer\", padding=(10, 10))\n",
"answer_frame.pack(fill=\"both\", expand=True, padx=10, pady=10)\n",
"\n",
"# Create a frame to hold the text widget and scrollbar\n",
"text_frame = ttk.Frame(answer_frame)\n",
"text_frame.pack(fill=\"both\", expand=True)\n",
"\n",
"# Create the text widget and scrollbar\n",
"answer_text = tk.Text(text_frame, wrap=tk.WORD, width=70, height=100)\n",
"scrollbar = ttk.Scrollbar(text_frame, orient=\"vertical\", command=answer_text.yview)\n",
"answer_text.configure(yscrollcommand=scrollbar.set)\n",
"\n",
"# Pack the text widget and scrollbar\n",
"answer_text.pack(side=\"left\", fill=\"both\", expand=True)\n",
"scrollbar.pack(side=\"right\", fill=\"y\")\n",
"\n",
"# Set initial text and disable editing\n",
"answer_text.insert(tk.END, \"Your answer will appear here.\")\n",
"answer_text.configure(state='disabled')\n",
"\n",
"# Run the main event loop\n",
"root.mainloop()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "llms",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

233
week1/community-contributions/day1-research-paper-summarization.ipynb

@ -0,0 +1,233 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "1b8f7ac7-7089-427a-8f63-57211da7e691",
"metadata": {},
"source": [
"## Summarizing Research Papers"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "641d5c00-ff09-4697-9c87-5de5df1469f8",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display\n",
"from openai import OpenAI\n",
"\n",
"# If you get an error running this cell, then please head over to the troubleshooting notebook!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1a6a2864-fd9d-43e2-b0ca-1476c0153077",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"\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!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "340e3166-5aa7-4bcf-9cf0-e2fc776dc322",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73198fb7-581f-42ac-99a6-76c56c86248d",
"metadata": {},
"outputs": [],
"source": [
"# A class to represent a Webpage\n",
"# If you're not familiar with Classes, check out the \"Intermediate Python\" notebook\n",
"\n",
"# Some websites need you to use proper headers when fetching them:\n",
"headers = {\n",
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
"}\n",
"\n",
"class Paper:\n",
"\n",
" def __init__(self, url):\n",
" \"\"\"\n",
" Create this Website object from the given url using the BeautifulSoup library\n",
" \"\"\"\n",
" self.url = url\n",
" response = requests.get(url, headers=headers)\n",
" soup = BeautifulSoup(response.content, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3b39c3ad-d238-418e-9e6a-55a4fd717ebc",
"metadata": {},
"outputs": [],
"source": [
"#Insert Paper URL\n",
"res = Paper(\" \")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "83bc1eec-4187-4c6c-b188-3f72564351f1",
"metadata": {},
"outputs": [],
"source": [
"system_prompt = \"\"\"You are a research paper summarizer. You take the url of the research paper and extract the following:\n",
"1) Title and Author of the research paper.\n",
"2) Year it was published it\n",
"3) Objective or aim of the research to specify why the research was conducted\n",
"4) Background or Introduction to explain the need to conduct this research or any topics the readers must have knowledge about\n",
"5) Type of research/study/experiment to explain what kind of research it is.\n",
"6) Methods or methodology to explain what the researchers did to conduct the research\n",
"7) Results and key findings to explain what the researchers found\n",
"8) Conclusion tells about the conclusions that can be drawn from this research including limitations and future direction\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4aba1b51-9a72-4325-8c86-3968b9d3172e",
"metadata": {},
"outputs": [],
"source": [
"# A function that writes a User Prompt that asks for summaries of websites:\n",
"\n",
"def user_prompt_for(paper):\n",
" user_prompt = f\"You are looking at a website titled {paper.title}\"\n",
" user_prompt += \"\\nThe contents of this paper is as follows; \\\n",
"please provide a short summary of this paper in markdown. \\\n",
"If it includes additional headings, then summarize these too.\\n\\n\"\n",
" user_prompt += paper.text\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "659cb3c4-8a02-493d-abe7-20da9219e358",
"metadata": {},
"outputs": [],
"source": [
"# See how this function creates exactly the format above\n",
"def messages_for(paper):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(paper)}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08ea1193-1bbb-40de-ba64-d02ffe109372",
"metadata": {},
"outputs": [],
"source": [
"messages_for(res)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e07d00e7-1b87-4ca8-a69d-4a206e34a2b2",
"metadata": {},
"outputs": [],
"source": [
"# And now: call the OpenAI API. You will get very familiar with this!\n",
"\n",
"def summarize(url):\n",
" paper = Paper(url)\n",
" response = openai.chat.completions.create(\n",
" model = \"gpt-4o-mini\",\n",
" messages = messages_for(paper)\n",
" )\n",
" return response.choices[0].message.content"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c12df95-1700-47ee-891b-96b0a7227bdd",
"metadata": {},
"outputs": [],
"source": [
"# A function to display this nicely in the Jupyter output, using markdown\n",
"\n",
"def display_summary(url):\n",
" summary = summarize(url)\n",
" display(Markdown(summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05cff05f-2b74-44a4-9dbd-57c08f8f56cb",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# Insert Paper URL in the quotes below\n",
"display_summary(\" \")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

224
week1/community-contributions/day1-webscraping-playwright.ipynb

@ -0,0 +1,224 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display\n",
"from openai import OpenAI\n",
"\n",
"# If you get an error running this cell, then please head over to the troubleshooting notebook!"
]
},
{
"cell_type": "markdown",
"id": "6900b2a8-6384-4316-8aaa-5e519fca4254",
"metadata": {},
"source": [
"# Connecting to OpenAI\n",
"\n",
"The next cell is where we load in the environment variables in your `.env` file and connect to OpenAI.\n",
"\n",
"## Troubleshooting if you have problems:\n",
"\n",
"Head over to the [troubleshooting](troubleshooting.ipynb) notebook in this folder for step by step code to identify the root cause and fix it!\n",
"\n",
"If you make a change, try restarting the \"Kernel\" (the python process sitting behind this notebook) by Kernel menu >> Restart Kernel and Clear Outputs of All Cells. Then try this notebook again, starting at the top.\n",
"\n",
"Or, contact me! Message me or email ed@edwarddonner.com and we will get this to work.\n",
"\n",
"Any concerns about API costs? See my notes in the README - costs should be minimal, and you can control it at every point. You can also use Ollama as a free alternative, which we discuss during Day 2."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b87cadb-d513-4303-baee-a37b6f938e4d",
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables in a file called .env\n",
"\n",
"load_dotenv()\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!\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "019974d9-f3ad-4a8a-b5f9-0a3719aea2d3",
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"\n",
"# If this doesn't work, try Kernel menu >> Restart Kernel and Clear Outputs Of All Cells, then run the cells from the top of this notebook down.\n",
"# If it STILL doesn't work (horrors!) then please see the troubleshooting notebook, or try the below line instead:\n",
"# openai = OpenAI(api_key=\"your-key-here-starting-sk-proj-\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abdb8417-c5dc-44bc-9bee-2e059d162699",
"metadata": {},
"outputs": [],
"source": [
"# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\n",
"\n",
"system_prompt = \"You are an assistant that analyzes the contents of a website \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"Respond in markdown.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0275b1b-7cfe-4f9d-abfa-7650d378da0c",
"metadata": {},
"outputs": [],
"source": [
"# A function that writes a User Prompt that asks for summaries of websites:\n",
"\n",
"def user_prompt_for(website):\n",
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
" user_prompt += \"\\nThe contents of this website is as follows; \\\n",
"please provide a short summary of this website in markdown. \\\n",
"If it includes news or announcements, then summarize these too.\\n\\n\"\n",
" user_prompt += website.text\n",
" return user_prompt"
]
},
{
"cell_type": "markdown",
"id": "36ed9f14-b349-40e9-a42c-b367e77f8bda",
"metadata": {},
"source": [
"## An extra exercise for those who enjoy web scraping\n",
"\n",
"You may notice that if you try the course example with \"https://openai.com\" - it doesn't work! That's because OpenAI has a fancy website that uses Javascript. There are many ways around this that some of you might be familiar with. Below an example created with Playwright."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dca2768e",
"metadata": {},
"outputs": [],
"source": [
"! pip install playwright\n",
"! playwright install"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "682eff74-55c4-4d4b-b267-703edbc293c7",
"metadata": {},
"outputs": [],
"source": [
"import asyncio\n",
"from playwright.async_api import async_playwright\n",
"import nest_asyncio\n",
"from bs4 import BeautifulSoup\n",
"import time\n",
"\n",
"nest_asyncio.apply()\n",
"\n",
"class Website:\n",
" title: str\n",
" text: str\n",
" url: str\n",
"\n",
" def __init__(self, url):\n",
" self.url = url\n",
" \n",
" async def run(self, playwright):\n",
" browser = await playwright.chromium.launch(headless=False)\n",
" page = await browser.new_page()\n",
" await page.goto(self.url)\n",
" await page.wait_for_load_state('load')\n",
" \n",
" # Extract data from the page\n",
" self.title = await page.title()\n",
" text = await page.content()\n",
" await browser.close()\n",
" \n",
" soup = BeautifulSoup(text, 'html.parser')\n",
" for irrelevant in soup([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.get_text(separator=\"\\n\", strip=True)\n",
" \n",
" async def main(self):\n",
" async with async_playwright() as playwright:\n",
" await self.run(playwright) \n",
" \n",
"def messages_for(website):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
" ]\n",
"\n",
"if __name__ == \"__main__\":\n",
" site = Website('https://openai.com')\n",
" asyncio.run(site.main())\n",
" response = openai.chat.completions.create(\n",
" model = \"gpt-4o-mini\",\n",
" messages = messages_for(site)\n",
" )\n",
"\n",
" web_summary = response.choices[0].message.content\n",
" display(Markdown(web_summary))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69218dec-749c-412d-84a0-40a10fd80c73",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

192
week1/community-contributions/day2 EXERCISE-Summarization-Ollama.ipynb

@ -0,0 +1,192 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e3ce0a59-fbfb-4377-85db-f62f95039200",
"metadata": {},
"source": [
"# Day2 EXERCISE - Summarization using Ollama"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e2a9393-7767-488e-a8bf-27c12dca35bd",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"from dotenv import load_dotenv\n",
"import requests\n",
"from bs4 import BeautifulSoup\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29ddd15d-a3c5-4f4e-a678-873f56162724",
"metadata": {},
"outputs": [],
"source": [
"# Constants\n",
"\n",
"OLLAMA_API = \"http://localhost:11434/api/chat\"\n",
"HEADERS = {\"Content-Type\": \"application/json\"}\n",
"MODEL = \"llama3.2\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb5c0f84-4e4d-4f87-b492-e09d0333a638",
"metadata": {},
"outputs": [],
"source": [
"# A class to represent a Webpage\n",
"# If you're not familiar with Classes, check out the \"Intermediate Python\" notebook\n",
"\n",
"# Some websites need you to use proper headers when fetching them:\n",
"headers = {\n",
" \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36\"\n",
"}\n",
"\n",
"class Website:\n",
"\n",
" def __init__(self, url):\n",
" \"\"\"\n",
" Create this Website object from the given url using the BeautifulSoup library\n",
" \"\"\"\n",
" self.url = url\n",
" response = requests.get(url, headers=headers)\n",
" soup = BeautifulSoup(response.content, 'html.parser')\n",
" self.title = soup.title.string if soup.title else \"No title found\"\n",
" for irrelevant in soup.body([\"script\", \"style\", \"img\", \"input\"]):\n",
" irrelevant.decompose()\n",
" self.text = soup.body.get_text(separator=\"\\n\", strip=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "23457b52-c85b-4dc1-b946-6f1461dc0675",
"metadata": {},
"outputs": [],
"source": [
"\n",
"ed = Website(\"https://edwarddonner.com\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bed206ed-43c1-4f68-ad01-a738b3b4648d",
"metadata": {},
"outputs": [],
"source": [
"# Define our system prompt - you can experiment with this later, changing the last sentence to 'Respond in markdown in Spanish.\"\n",
"\n",
"system_prompt = \"You are an assistant that analyzes the contents of a website \\\n",
"and provides a short summary, ignoring text that might be navigation related. \\\n",
"Respond in markdown.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e558f381-614a-461f-83bc-e5bdc99460df",
"metadata": {},
"outputs": [],
"source": [
"# A function that writes a User Prompt that asks for summaries of websites:\n",
"\n",
"def user_prompt_for(website):\n",
" user_prompt = f\"You are looking at a website titled {website.title}\"\n",
" user_prompt += \"\\nThe contents of this website is as follows; \\\n",
"please provide a short summary of this website in markdown. \\\n",
"If it includes news or announcements, then summarize these too.\\n\\n\"\n",
" user_prompt += website.text\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5ba638d-aeb9-441e-a62a-8e8027ad8439",
"metadata": {},
"outputs": [],
"source": [
"# See how this function creates exactly the format above\n",
"\n",
"def messages_for(website):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_prompt},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(website)}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e85ca2ec-3e46-4b8f-9c2f-66e7d20138fa",
"metadata": {},
"outputs": [],
"source": [
"#website search\n",
"\n",
"ed = Website(\"https://edwarddonner.com\")\n",
"messages=messages_for(ed)\n",
"\n",
"payload = {\n",
" \"model\": MODEL,\n",
" \"messages\": messages,\n",
" \"stream\": False\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7745b9c4-57dc-4867-9180-61fa5db55eb8",
"metadata": {},
"outputs": [],
"source": [
"import ollama\n",
"\n",
"response = ollama.chat(model=MODEL, messages=messages)\n",
"print(response['message']['content'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "402d5686-4e76-4110-b65a-b3906c35c0a4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

182
week2/community-contributions/day3-gradio-auth.ipynb

@ -0,0 +1,182 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import Required Libraries"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import gradio as gr"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load Environment Variables"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"load_dotenv()\n",
"openai_api_key = os.getenv('OPENAI_API_KEY')\n",
"if not openai_api_key:\n",
" print(\"OpenAI API Key not set\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Initialize OpenAI Client and Define Model"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"openai = OpenAI()\n",
"MODEL = 'gpt-4o-mini'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the System Message"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"system_message = (\n",
" \"You are a helpful assistant, trying your best to answer every question as accurately as possible. \"\n",
" \"You are also free to say you do not know if you do not have the information to answer a question. \"\n",
" \"You always respond in markdown.\"\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define the Chat Function"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"def chat(message, history):\n",
" messages = [{\"role\": \"system\", \"content\": system_message}] + history + [{\"role\": \"user\", \"content\": message}]\n",
"\n",
" stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n",
"\n",
" response = \"\"\n",
" for chunk in stream:\n",
" response += chunk.choices[0].delta.content or ''\n",
" yield response"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the Chat Interface"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"demo = gr.ChatInterface(\n",
" fn=chat,\n",
" title=\"AI chatbot\",\n",
" description=\"Please login to use the chat interface\",\n",
" type='messages',\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"auth_data is a list of tuples, where each tuple contains a username and password."
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"auth_data = [(\"user_1\", \"password_1\"), (\"user_2\", \"password_2\"), (\"user_3\", \"password_3\")]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add Authentication and Launch\n",
"\n",
"auth_message is the message displayed to users before accessing the interface."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"demo.launch(share=True,\n",
" auth=auth_data,\n",
" auth_message=\"Please enter your credentials to access the chat interface\",\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "llms",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

908
week4/community-contributions/day4-gemini-included.ipynb

@ -0,0 +1,908 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "4a6ab9a2-28a2-445d-8512-a0dc8d1b54e9",
"metadata": {},
"source": [
"# Code Generator\n",
"\n",
"The requirement: use an Open Source model to generate high performance C++ code from Python code\n",
"\n",
"To replicate this, you'll need to set up a HuggingFace endpoint as I do in the video. It's simple to do, and it's quite satisfying to see the results!\n",
"\n",
"It's also an important part of your learning; this is the first example of deploying an open source model to be behind an API. We'll return to this in Week 8, but this should plant a seed in your mind for what's involved in moving open source models into production."
]
},
{
"cell_type": "markdown",
"id": "22e1567b-33fd-49e7-866e-4b635d15715a",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left;\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../../important.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h1 style=\"color:#900;\">Important - Pause Endpoints when not in use</h1>\n",
" <span style=\"color:#900;\">\n",
" If you do decide to use HuggingFace endpoints for this project, you should stop or pause the endpoints when you are done to avoid accruing unnecessary running cost. The costs are very low as long as you only run the endpoint when you're using it. Navigate to the HuggingFace endpoint UI <a href=\"https://ui.endpoints.huggingface.co/\">here,</a> open your endpoint, and click Pause to put it on pause so you no longer pay for it. \n",
"Many thanks to student John L. for raising this.\n",
"<br/><br/>\n",
"In week 8 we will use Modal instead of HuggingFace endpoints; with Modal you only pay for the time that you use it and you should get free credits.\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e610bf56-a46e-4aff-8de1-ab49d62b1ad3",
"metadata": {},
"outputs": [],
"source": [
"# imports\n",
"\n",
"import os\n",
"import io\n",
"import sys\n",
"import json\n",
"import requests\n",
"from dotenv import load_dotenv\n",
"from openai import OpenAI\n",
"import google.generativeai as genai\n",
"import anthropic\n",
"from IPython.display import Markdown, display, update_display\n",
"import gradio as gr\n",
"import subprocess"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f672e1c-87e9-4865-b760-370fa605e614",
"metadata": {},
"outputs": [],
"source": [
"# environment\n",
"\n",
"load_dotenv()\n",
"os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['ANTHROPIC_API_KEY'] = os.getenv('ANTHROPIC_API_KEY', 'your-key-if-not-using-env')\n",
"os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN', 'your-key-if-not-using-env')\n",
"os.environ['GOOGLE_API_KEY'] = os.getenv('GOOGLE_API_KEY', 'your-key-if-not-using-env')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8aa149ed-9298-4d69-8fe2-8f5de0f667da",
"metadata": {},
"outputs": [],
"source": [
"# initialize\n",
"\n",
"openai = OpenAI()\n",
"claude = anthropic.Anthropic()\n",
"OPENAI_MODEL = \"gpt-4o\"\n",
"CLAUDE_MODEL = \"claude-3-5-sonnet-20240620\"\n",
"GEMINI_MODEL = 'gemini-1.5-pro'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6896636f-923e-4a2c-9d6c-fac07828a201",
"metadata": {},
"outputs": [],
"source": [
"system_message = \"You are an assistant that reimplements Python code in high performance C++ for an M1 Mac. \"\n",
"system_message += \"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. \"\n",
"system_message += \"The C++ response needs to produce an identical output in the fastest possible time. Keep implementations of random number generators identical so that results match exactly.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8e7b3546-57aa-4c29-bc5d-f211970d04eb",
"metadata": {},
"outputs": [],
"source": [
"def user_prompt_for(python):\n",
" user_prompt = \"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. \"\n",
" user_prompt += \"Respond only with C++ code; do not explain your work other than a few comments. \"\n",
" user_prompt += \"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\\n\\n\"\n",
" user_prompt += python\n",
" return user_prompt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c6190659-f54c-4951-bef4-4960f8e51cc4",
"metadata": {},
"outputs": [],
"source": [
"def messages_for(python):\n",
" return [\n",
" {\"role\": \"system\", \"content\": system_message},\n",
" {\"role\": \"user\", \"content\": user_prompt_for(python)}\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "71e1ba8c-5b05-4726-a9f3-8d8c6257350b",
"metadata": {},
"outputs": [],
"source": [
"# write to a file called optimized.cpp\n",
"\n",
"def write_output(cpp):\n",
" code = cpp.replace(\"```cpp\",\"\").replace(\"```\",\"\")\n",
" with open(\"optimized.cpp\", \"w\") as f:\n",
" f.write(code)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7d2fea8-74c6-4421-8f1e-0e76d5b201b9",
"metadata": {},
"outputs": [],
"source": [
"def optimize_gpt(python): \n",
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
" reply = \"\"\n",
" for chunk in stream:\n",
" fragment = chunk.choices[0].delta.content or \"\"\n",
" reply += fragment\n",
" print(fragment, end='', flush=True)\n",
" write_output(reply)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7cd84ad8-d55c-4fe0-9eeb-1895c95c4a9d",
"metadata": {},
"outputs": [],
"source": [
"def optimize_claude(python):\n",
" result = claude.messages.stream(\n",
" model=CLAUDE_MODEL,\n",
" max_tokens=2000,\n",
" system=system_message,\n",
" messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
" )\n",
" reply = \"\"\n",
" with result as stream:\n",
" for text in stream.text_stream:\n",
" reply += text\n",
" print(text, end=\"\", flush=True)\n",
" write_output(reply)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3625fcd6-209f-481c-a745-dcbcf5e44bc1",
"metadata": {},
"outputs": [],
"source": [
"def optimize_gemini(python):\n",
" gemini = genai.GenerativeModel(\n",
" model_name = GEMINI_MODEL,\n",
" system_instruction=system_message\n",
" )\n",
" response = gemini.generate_content(\n",
" user_prompt_for(python),\n",
" stream=True\n",
" )\n",
" reply = \"\"\n",
" for chunk in response:\n",
" reply += chunk.text\n",
" print(chunk.text, end=\"\", flush=True)\n",
" write_output(reply)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1cbb778-fa57-43de-b04b-ed523f396c38",
"metadata": {},
"outputs": [],
"source": [
"pi = \"\"\"\n",
"import time\n",
"\n",
"def calculate(iterations, param1, param2):\n",
" result = 1.0\n",
" for i in range(1, iterations+1):\n",
" j = i * param1 - param2\n",
" result -= (1/j)\n",
" j = i * param1 + param2\n",
" result += (1/j)\n",
" return result\n",
"\n",
"start_time = time.time()\n",
"result = calculate(100_000_000, 4, 1) * 4\n",
"end_time = time.time()\n",
"\n",
"print(f\"Result: {result:.12f}\")\n",
"print(f\"Execution Time: {(end_time - start_time):.6f} seconds\")\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fe891e3a-d1c4-4ee5-a361-34d0982fcff4",
"metadata": {},
"outputs": [],
"source": [
"optimize_gemini(pi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7fe1cd4b-d2c5-4303-afed-2115a3fef200",
"metadata": {},
"outputs": [],
"source": [
"exec(pi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "105db6f9-343c-491d-8e44-3a5328b81719",
"metadata": {},
"outputs": [],
"source": [
"optimize_gpt(pi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf26ee95-0c77-491d-9a91-579a1e96a8a3",
"metadata": {},
"outputs": [],
"source": [
"exec(pi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4194e40c-04ab-4940-9d64-b4ad37c5bb40",
"metadata": {},
"outputs": [],
"source": [
"!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
"!./optimized"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "983a11fe-e24d-4c65-8269-9802c5ef3ae6",
"metadata": {},
"outputs": [],
"source": [
"optimize_claude(pi)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5a766f9-3d23-4bb4-a1d4-88ec44b61ddf",
"metadata": {},
"outputs": [],
"source": [
"!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
"!./optimized"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c3b497b3-f569-420e-b92e-fb0f49957ce0",
"metadata": {},
"outputs": [],
"source": [
"python_hard = \"\"\"# Be careful to support large number sizes\n",
"\n",
"def lcg(seed, a=1664525, c=1013904223, m=2**32):\n",
" value = seed\n",
" while True:\n",
" value = (a * value + c) % m\n",
" yield value\n",
" \n",
"def max_subarray_sum(n, seed, min_val, max_val):\n",
" lcg_gen = lcg(seed)\n",
" random_numbers = [next(lcg_gen) % (max_val - min_val + 1) + min_val for _ in range(n)]\n",
" max_sum = float('-inf')\n",
" for i in range(n):\n",
" current_sum = 0\n",
" for j in range(i, n):\n",
" current_sum += random_numbers[j]\n",
" if current_sum > max_sum:\n",
" max_sum = current_sum\n",
" return max_sum\n",
"\n",
"def total_max_subarray_sum(n, initial_seed, min_val, max_val):\n",
" total_sum = 0\n",
" lcg_gen = lcg(initial_seed)\n",
" for _ in range(20):\n",
" seed = next(lcg_gen)\n",
" total_sum += max_subarray_sum(n, seed, min_val, max_val)\n",
" return total_sum\n",
"\n",
"# Parameters\n",
"n = 10000 # Number of random numbers\n",
"initial_seed = 42 # Initial seed for the LCG\n",
"min_val = -10 # Minimum value of random numbers\n",
"max_val = 10 # Maximum value of random numbers\n",
"\n",
"# Timing the function\n",
"import time\n",
"start_time = time.time()\n",
"result = total_max_subarray_sum(n, initial_seed, min_val, max_val)\n",
"end_time = time.time()\n",
"\n",
"print(\"Total Maximum Subarray Sum (20 runs):\", result)\n",
"print(\"Execution Time: {:.6f} seconds\".format(end_time - start_time))\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dab5e4bc-276c-4555-bd4c-12c699d5e899",
"metadata": {},
"outputs": [],
"source": [
"exec(python_hard)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e8d24ed5-2c15-4f55-80e7-13a3952b3cb8",
"metadata": {},
"outputs": [],
"source": [
"optimize_gpt(python_hard)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0b3d073-88a2-40b2-831c-6f0c345c256f",
"metadata": {},
"outputs": [],
"source": [
"!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
"!./optimized"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e9305446-1d0c-4b51-866a-b8c1e299bf5c",
"metadata": {},
"outputs": [],
"source": [
"optimize_claude(python_hard)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0c181036-8193-4fdd-aef3-fc513b218d43",
"metadata": {},
"outputs": [],
"source": [
"!clang++ -O3 -std=c++17 -march=armv8.3-a -o optimized optimized.cpp\n",
"!./optimized"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0be9f47d-5213-4700-b0e2-d444c7c738c0",
"metadata": {},
"outputs": [],
"source": [
"def stream_gpt(python): \n",
" stream = openai.chat.completions.create(model=OPENAI_MODEL, messages=messages_for(python), stream=True)\n",
" reply = \"\"\n",
" for chunk in stream:\n",
" fragment = chunk.choices[0].delta.content or \"\"\n",
" reply += fragment\n",
" yield reply.replace('```cpp\\n','').replace('```','')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8669f56b-8314-4582-a167-78842caea131",
"metadata": {},
"outputs": [],
"source": [
"def stream_claude(python):\n",
" result = claude.messages.stream(\n",
" model=CLAUDE_MODEL,\n",
" max_tokens=2000,\n",
" system=system_message,\n",
" messages=[{\"role\": \"user\", \"content\": user_prompt_for(python)}],\n",
" )\n",
" reply = \"\"\n",
" with result as stream:\n",
" for text in stream.text_stream:\n",
" reply += text\n",
" yield reply.replace('```cpp\\n','').replace('```','')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9b6938f-89ef-4998-a334-2f6c042a2da4",
"metadata": {},
"outputs": [],
"source": [
"def stream_gemini(python):\n",
" gemini = genai.GenerativeModel(\n",
" model_name = GEMINI_MODEL,\n",
" system_instruction=system_message\n",
" )\n",
" response = gemini.generate_content(\n",
" user_prompt_for(python),\n",
" stream=True\n",
" )\n",
" reply = \"\"\n",
" for chunk in response:\n",
" reply += chunk.text\n",
" yield reply.replace('```cpp\\n','').replace('```','')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f1ae8f5-16c8-40a0-aa18-63b617df078d",
"metadata": {},
"outputs": [],
"source": [
"def optimize(python, model):\n",
" if model==\"GPT\":\n",
" result = stream_gpt(python)\n",
" elif model==\"Claude\":\n",
" result = stream_claude(python)\n",
" elif model==\"Gemini\":\n",
" result= stream_gemini(python)\n",
" else:\n",
" raise ValueError(\"Unknown model\")\n",
" for stream_so_far in result:\n",
" yield stream_so_far "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1ddb38e-6b0a-4c37-baa4-ace0b7de887a",
"metadata": {},
"outputs": [],
"source": [
"with gr.Blocks() as ui:\n",
" with gr.Row():\n",
" python = gr.Textbox(label=\"Python code:\", lines=10, value=python_hard)\n",
" cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
" with gr.Row():\n",
" model = gr.Dropdown([\"GPT\", \"Claude\",\"Gemini\"], label=\"Select model\", value=\"GPT\")\n",
" convert = gr.Button(\"Convert code\")\n",
"\n",
" convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19bf2bff-a822-4009-a539-f003b1651383",
"metadata": {},
"outputs": [],
"source": [
"def execute_python(code):\n",
" try:\n",
" output = io.StringIO()\n",
" sys.stdout = output\n",
" exec(code)\n",
" finally:\n",
" sys.stdout = sys.__stdout__\n",
" return output.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77f3ab5d-fcfb-4d3f-8728-9cacbf833ea6",
"metadata": {},
"outputs": [],
"source": [
"def execute_cpp(code):\n",
" write_output(code)\n",
" try:\n",
" compile_result = subprocess.run(compiler_cmd[2], check=True, text=True, capture_output=True)\n",
" run_cmd = [\"./optimized\"]\n",
" run_result = subprocess.run(run_cmd, check=True, text=True, capture_output=True)\n",
" return run_result.stdout\n",
" except subprocess.CalledProcessError as e:\n",
" return f\"An error occurred:\\n{e.stderr}\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9a2274f1-d03b-42c0-8dcc-4ce159b18442",
"metadata": {},
"outputs": [],
"source": [
"css = \"\"\"\n",
".python {background-color: #306998;}\n",
".cpp {background-color: #050;}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f1303932-160c-424b-97a8-d28c816721b2",
"metadata": {},
"outputs": [],
"source": [
"with gr.Blocks(css=css) as ui:\n",
" gr.Markdown(\"## Convert code from Python to C++\")\n",
" with gr.Row():\n",
" python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
" cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
" with gr.Row():\n",
" model = gr.Dropdown([\"GPT\", \"Claude\",\"Gemini\"], label=\"Select model\", value=\"GPT\")\n",
" with gr.Row():\n",
" convert = gr.Button(\"Convert code\")\n",
" with gr.Row():\n",
" python_run = gr.Button(\"Run Python\")\n",
" cpp_run = gr.Button(\"Run C++\")\n",
" with gr.Row():\n",
" python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
" cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
"\n",
" convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
" python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
" cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb8c5b4e-ec51-4f21-b3f8-6aa94fede86d",
"metadata": {},
"outputs": [],
"source": [
"from huggingface_hub import login, InferenceClient\n",
"from transformers import AutoTokenizer"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "13347633-4606-4e38-9927-80c39e65c1f1",
"metadata": {},
"outputs": [],
"source": [
"hf_token = os.environ['HF_TOKEN']\n",
"login(hf_token, add_to_git_credential=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef60a4df-6267-4ebd-8eed-dcb917af0a5e",
"metadata": {},
"outputs": [],
"source": [
"code_qwen = \"Qwen/CodeQwen1.5-7B-Chat\"\n",
"code_gemma = \"google/codegemma-7b-it\"\n",
"CODE_QWEN_URL = \"https://h1vdol7jxhje3mpn.us-east-1.aws.endpoints.huggingface.cloud\"\n",
"CODE_GEMMA_URL = \"https://c5hggiyqachmgnqg.us-east-1.aws.endpoints.huggingface.cloud\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "695ce389-a903-4533-a2f1-cd9e2a6af8f2",
"metadata": {},
"outputs": [],
"source": [
"tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
"messages = messages_for(pi)\n",
"text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4548e96-0b32-4793-bdd6-1b072c2f26ab",
"metadata": {},
"outputs": [],
"source": [
"print(text)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb2a126b-09e7-4966-bc97-0ef5c2cc7896",
"metadata": {},
"outputs": [],
"source": [
"client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
"stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
"for r in stream:\n",
" print(r.token.text, end = \"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "127a52e5-ad85-42b7-a0f5-9afda5efe090",
"metadata": {},
"outputs": [],
"source": [
"def stream_code_qwen(python):\n",
" tokenizer = AutoTokenizer.from_pretrained(code_qwen)\n",
" messages = messages_for(python)\n",
" text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)\n",
" client = InferenceClient(CODE_QWEN_URL, token=hf_token)\n",
" stream = client.text_generation(text, stream=True, details=True, max_new_tokens=3000)\n",
" result = \"\"\n",
" for r in stream:\n",
" result += r.token.text\n",
" yield result "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a82387d1-7651-4923-995b-fe18356fcaa6",
"metadata": {},
"outputs": [],
"source": [
"def optimize(python, model):\n",
" if model==\"GPT\":\n",
" result = stream_gpt(python)\n",
" elif model==\"Claude\":\n",
" result = stream_claude(python)\n",
" elif model==\"Gemini\":\n",
" result= stream_gemini(python)\n",
" elif model==\"CodeQwen\":\n",
" result = stream_code_qwen(python)\n",
" else:\n",
" raise ValueError(\"Unknown model\")\n",
" for stream_so_far in result:\n",
" yield stream_so_far "
]
},
{
"cell_type": "markdown",
"id": "4b0a6a97-5b8a-4a9b-8ee0-7561e0ced673",
"metadata": {},
"source": [
"<table style=\"margin: 0; text-align: left;\">\n",
" <tr>\n",
" <td style=\"width: 150px; height: 150px; vertical-align: middle;\">\n",
" <img src=\"../../thankyou.jpg\" width=\"150\" height=\"150\" style=\"display: block;\" />\n",
" </td>\n",
" <td>\n",
" <h2 style=\"color:#090;\">Thank you to @CloudLlama for an amazing contribution</h2>\n",
" <span style=\"color:#090;\">\n",
" A student has contributed a chunk of code to improve this, in the next 2 cells. You can now select which Python porgram to run,\n",
" and a compiler is automatically selected that will work on PC, Windows and Mac. Massive thank you @CloudLlama!\n",
" </span>\n",
" </td>\n",
" </tr>\n",
"</table>"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ba311ec-c16a-4fe0-946b-4b940704cf65",
"metadata": {},
"outputs": [],
"source": [
"def select_sample_program(sample_program):\n",
" if sample_program==\"pi\":\n",
" return pi\n",
" elif sample_program==\"python_hard\":\n",
" return python_hard\n",
" else:\n",
" return \"Type your Python program here\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e42286bc-085c-45dc-b101-234308e58269",
"metadata": {},
"outputs": [],
"source": [
"import platform\n",
"\n",
"VISUAL_STUDIO_2022_TOOLS = \"C:\\\\Program Files\\\\Microsoft Visual Studio\\\\2022\\\\Community\\\\Common7\\Tools\\\\VsDevCmd.bat\"\n",
"VISUAL_STUDIO_2019_TOOLS = \"C:\\\\Program Files (x86)\\\\Microsoft Visual Studio\\\\2019\\\\BuildTools\\\\Common7\\\\Tools\\\\VsDevCmd.bat\"\n",
"\n",
"simple_cpp = \"\"\"\n",
"#include <iostream>\n",
"\n",
"int main() {\n",
" std::cout << \"Hello\";\n",
" return 0;\n",
"}\n",
"\"\"\"\n",
"\n",
"def run_cmd(command_to_run):\n",
" try:\n",
" run_result = subprocess.run(command_to_run, check=True, text=True, capture_output=True)\n",
" return run_result.stdout if run_result.stdout else \"SUCCESS\"\n",
" except:\n",
" return \"\"\n",
"\n",
"def c_compiler_cmd(filename_base):\n",
" my_platform = platform.system()\n",
" my_compiler = []\n",
"\n",
" try:\n",
" with open(\"simple.cpp\", \"w\") as f:\n",
" f.write(simple_cpp)\n",
" \n",
" if my_platform == \"Windows\":\n",
" if os.path.isfile(VISUAL_STUDIO_2022_TOOLS):\n",
" if os.path.isfile(\"./simple.exe\"):\n",
" os.remove(\"./simple.exe\")\n",
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
" my_compiler = [\"Windows\", \"Visual Studio 2022\", [\"cmd\", \"/c\", VISUAL_STUDIO_2022_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
" \n",
" if not my_compiler:\n",
" if os.path.isfile(VISUAL_STUDIO_2019_TOOLS):\n",
" if os.path.isfile(\"./simple.exe\"):\n",
" os.remove(\"./simple.exe\")\n",
" compile_cmd = [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", \"simple.cpp\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple.exe\"]) == \"Hello\":\n",
" my_compiler = [\"Windows\", \"Visual Studio 2019\", [\"cmd\", \"/c\", VISUAL_STUDIO_2019_TOOLS, \"&\", \"cl\", f\"{filename_base}.cpp\"]]\n",
" \n",
" if not my_compiler:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" \n",
" elif my_platform == \"Linux\":\n",
" if os.path.isfile(\"./simple\"):\n",
" os.remove(\"./simple\")\n",
" compile_cmd = [\"g++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
" my_compiler = [\"Linux\", \"GCC (g++)\", [\"g++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\" ]]\n",
" \n",
" if not my_compiler:\n",
" if os.path.isfile(\"./simple\"):\n",
" os.remove(\"./simple\")\n",
" compile_cmd = [\"clang++\", \"simple.cpp\", \"-o\", \"simple\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
" my_compiler = [\"Linux\", \"Clang++\", [\"clang++\", f\"{filename_base}.cpp\", \"-o\", f\"{filename_base}\"]]\n",
" \n",
" if not my_compiler:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" \n",
" elif my_platform == \"Darwin\":\n",
" if os.path.isfile(\"./simple\"):\n",
" os.remove(\"./simple\")\n",
" compile_cmd = [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", \"simple\", \"simple.cpp\"]\n",
" if run_cmd(compile_cmd):\n",
" if run_cmd([\"./simple\"]) == \"Hello\":\n",
" my_compiler = [\"Macintosh\", \"Clang++\", [\"clang++\", \"-Ofast\", \"-std=c++17\", \"-march=armv8.5-a\", \"-mtune=apple-m1\", \"-mcpu=apple-m1\", \"-o\", f\"{filename_base}\", f\"{filename_base}.cpp\"]]\n",
" \n",
" if not my_compiler:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" except:\n",
" my_compiler=[my_platform, \"Unavailable\", []]\n",
" \n",
" if my_compiler:\n",
" return my_compiler\n",
" else:\n",
" return [\"Unknown\", \"Unavailable\", []]\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f9ca2e6f-60c1-4e5f-b570-63c75b2d189b",
"metadata": {},
"outputs": [],
"source": [
"compiler_cmd = c_compiler_cmd(\"optimized\")\n",
"\n",
"with gr.Blocks(css=css) as ui:\n",
" gr.Markdown(\"## Convert code from Python to C++\")\n",
" with gr.Row():\n",
" python = gr.Textbox(label=\"Python code:\", value=python_hard, lines=10)\n",
" cpp = gr.Textbox(label=\"C++ code:\", lines=10)\n",
" with gr.Row():\n",
" with gr.Column():\n",
" sample_program = gr.Radio([\"pi\", \"python_hard\"], label=\"Sample program\", value=\"python_hard\")\n",
" model = gr.Dropdown([\"GPT\", \"Claude\", \"Gemini\", \"CodeQwen\"], label=\"Select model\", value=\"GPT\")\n",
" with gr.Column():\n",
" architecture = gr.Radio([compiler_cmd[0]], label=\"Architecture\", interactive=False, value=compiler_cmd[0])\n",
" compiler = gr.Radio([compiler_cmd[1]], label=\"Compiler\", interactive=False, value=compiler_cmd[1])\n",
" with gr.Row():\n",
" convert = gr.Button(\"Convert code\")\n",
" with gr.Row():\n",
" python_run = gr.Button(\"Run Python\")\n",
" if not compiler_cmd[1] == \"Unavailable\":\n",
" cpp_run = gr.Button(\"Run C++\")\n",
" else:\n",
" cpp_run = gr.Button(\"No compiler to run C++\", interactive=False)\n",
" with gr.Row():\n",
" python_out = gr.TextArea(label=\"Python result:\", elem_classes=[\"python\"])\n",
" cpp_out = gr.TextArea(label=\"C++ result:\", elem_classes=[\"cpp\"])\n",
"\n",
" sample_program.change(select_sample_program, inputs=[sample_program], outputs=[python])\n",
" convert.click(optimize, inputs=[python, model], outputs=[cpp])\n",
" python_run.click(execute_python, inputs=[python], outputs=[python_out])\n",
" cpp_run.click(execute_cpp, inputs=[cpp], outputs=[cpp_out])\n",
"\n",
"ui.launch(inbrowser=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9d0ad093-425b-488e-8c3f-67f729dd9c06",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading…
Cancel
Save