dlnlm commited on
Commit
dd651f9
Β·
verified Β·
1 Parent(s): cb20fc0

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.ipynb +1 -0
  2. requirements.txt +4 -0
app.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyNAP9KrHYc02pXY1gI9aPlL"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","source":["!pip install groq gradio"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Fs5HxCHcXJNs","executionInfo":{"status":"ok","timestamp":1765606756706,"user_tz":-480,"elapsed":12831,"user":{"displayName":"Teoh Teik Toe","userId":"14648357212666722599"}},"outputId":"5798d080-e652-4252-fdb9-f875e76f8611"},"execution_count":1,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting groq\n"," Downloading groq-0.37.1-py3-none-any.whl.metadata (16 kB)\n","Requirement already satisfied: gradio in /usr/local/lib/python3.12/dist-packages (5.50.0)\n","Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.12/dist-packages (from groq) (4.12.0)\n","Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.12/dist-packages (from groq) (1.9.0)\n","Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.12/dist-packages (from groq) (0.28.1)\n","Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.12/dist-packages (from groq) (2.12.3)\n","Requirement already satisfied: sniffio in /usr/local/lib/python3.12/dist-packages (from groq) (1.3.1)\n","Requirement already satisfied: typing-extensions<5,>=4.10 in /usr/local/lib/python3.12/dist-packages (from groq) (4.15.0)\n","Requirement already satisfied: aiofiles<25.0,>=22.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (24.1.0)\n","Requirement already satisfied: brotli>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (1.2.0)\n","Requirement already satisfied: fastapi<1.0,>=0.115.2 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.118.3)\n","Requirement already satisfied: ffmpy in /usr/local/lib/python3.12/dist-packages (from gradio) (1.0.0)\n","Requirement already satisfied: gradio-client==1.14.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (1.14.0)\n","Requirement already satisfied: groovy~=0.1 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.1.2)\n","Requirement already satisfied: huggingface-hub<2.0,>=0.33.5 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.36.0)\n","Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (3.1.6)\n","Requirement already satisfied: markupsafe<4.0,>=2.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (3.0.3)\n","Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (2.0.2)\n","Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (3.11.5)\n","Requirement already satisfied: packaging in /usr/local/lib/python3.12/dist-packages (from gradio) (25.0)\n","Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (2.2.2)\n","Requirement already satisfied: pillow<12.0,>=8.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (11.3.0)\n","Requirement already satisfied: pydub in /usr/local/lib/python3.12/dist-packages (from gradio) (0.25.1)\n","Requirement already satisfied: python-multipart>=0.0.18 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.0.20)\n","Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (6.0.3)\n","Requirement already satisfied: ruff>=0.9.3 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.14.8)\n","Requirement already satisfied: safehttpx<0.2.0,>=0.1.6 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.1.7)\n","Requirement already satisfied: semantic-version~=2.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (2.10.0)\n","Requirement already satisfied: starlette<1.0,>=0.40.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.48.0)\n","Requirement already satisfied: tomlkit<0.14.0,>=0.12.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.13.3)\n","Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.20.0)\n","Requirement already satisfied: uvicorn>=0.14.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.38.0)\n","Requirement already satisfied: fsspec in /usr/local/lib/python3.12/dist-packages (from gradio-client==1.14.0->gradio) (2025.3.0)\n","Requirement already satisfied: websockets<16.0,>=13.0 in /usr/local/lib/python3.12/dist-packages (from gradio-client==1.14.0->gradio) (15.0.1)\n","Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.12/dist-packages (from anyio<5,>=3.5.0->groq) (3.11)\n","Requirement already satisfied: certifi in /usr/local/lib/python3.12/dist-packages (from httpx<1,>=0.23.0->groq) (2025.11.12)\n","Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx<1,>=0.23.0->groq) (1.0.9)\n","Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->groq) (0.16.0)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<2.0,>=0.33.5->gradio) (3.20.0)\n","Requirement already satisfied: requests in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<2.0,>=0.33.5->gradio) (2.32.4)\n","Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<2.0,>=0.33.5->gradio) (4.67.1)\n","Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<2.0,>=0.33.5->gradio) (1.2.0)\n","Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.12/dist-packages (from pandas<3.0,>=1.0->gradio) (2.9.0.post0)\n","Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas<3.0,>=1.0->gradio) (2025.2)\n","Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas<3.0,>=1.0->gradio) (2025.2)\n","Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic<3,>=1.9.0->groq) (0.7.0)\n","Requirement already satisfied: pydantic-core==2.41.4 in /usr/local/lib/python3.12/dist-packages (from pydantic<3,>=1.9.0->groq) (2.41.4)\n","Requirement already satisfied: typing-inspection>=0.4.2 in /usr/local/lib/python3.12/dist-packages (from pydantic<3,>=1.9.0->groq) (0.4.2)\n","Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.12/dist-packages (from typer<1.0,>=0.12->gradio) (8.3.1)\n","Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.12/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n","Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.12/dist-packages (from typer<1.0,>=0.12->gradio) (13.9.4)\n","Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.17.0)\n","Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.12/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (4.0.0)\n","Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.12/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.19.2)\n","Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests->huggingface-hub<2.0,>=0.33.5->gradio) (3.4.4)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests->huggingface-hub<2.0,>=0.33.5->gradio) (2.5.0)\n","Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.12/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n","Downloading groq-0.37.1-py3-none-any.whl (137 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m137.5/137.5 kB\u001b[0m \u001b[31m3.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hInstalling collected packages: groq\n","Successfully installed groq-0.37.1\n"]}]},{"cell_type":"code","execution_count":5,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":715},"id":"etQSYfeNXF9g","executionInfo":{"status":"ok","timestamp":1765609533202,"user_tz":-480,"elapsed":2798,"user":{"displayName":"Teoh Teik Toe","userId":"14648357212666722599"}},"outputId":"22ec2d61-112e-4237-81fe-80f2541a813f"},"outputs":[{"output_type":"stream","name":"stderr","text":["/tmp/ipython-input-485697301.py:60: UserWarning: You have not specified a value for the `type` parameter. Defaulting to the 'tuples' format for chatbot messages, but this is deprecated and will be removed in a future version of Gradio. Please set type='messages' instead, which uses openai-style dictionaries with 'role' and 'content' keys.\n"," chatbot = gr.Chatbot(height=400)\n","/tmp/ipython-input-485697301.py:60: DeprecationWarning: The default value of 'allow_tags' in gr.Chatbot will be changed from False to True in Gradio 6.0. You will need to explicitly set allow_tags=False if you want to disable tags in your chatbot.\n"," chatbot = gr.Chatbot(height=400)\n"]},{"output_type":"stream","name":"stdout","text":["It looks like you are running Gradio on a hosted Jupyter notebook, which requires `share=True`. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n","\n","Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n","* Running on public URL: https://784421a9c6cdb557f2.gradio.live\n","\n","This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"]},{"output_type":"display_data","data":{"text/plain":["<IPython.core.display.HTML object>"],"text/html":["<div><iframe src=\"https://784421a9c6cdb557f2.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"]},"metadata":{}}],"source":["import os\n","from groq import Groq\n","import gradio as gr\n","\n","# ---------- SET YOUR API KEY ----------\n","# Option 1: Set as environment variable before running:\n","# export GROQ_API_KEY=\"your_api_key_here\"\n","# Option 2: Put it directly here (less safe):\n","# os.environ[\"GROQ_API_KEY\"] = \"your_api_key_here\"\n","\n","#the below 3 lines is for colab, comment if you are using HF\n","from google.colab import userdata\n","GROQ_API_KEY = userdata.get('GROQ_API_KEY')\n","client = Groq(api_key=GROQ_API_KEY)\n","\n","#the below 1 lines is for HF, comment if you are using colab\n","#client = Groq(api_key=os.environ.get(\"GROQ_API_KEY\"))\n","\n","# Choose a Llama model available on Groq, e.g.:\n","MODEL_NAME = \"llama-3.3-70b-versatile\" # check console for latest names\n","\n","\n","def chat_with_groq(message, history):\n"," \"\"\"\n"," message: latest user input (string)\n"," history: list of [user, assistant] pairs from Gradio\n"," returns: assistant reply (string)\n"," \"\"\"\n","\n"," # Convert Gradio history into Groq-style messages\n"," messages = []\n"," for user_msg, bot_msg in history:\n"," messages.append({\"role\": \"user\", \"content\": user_msg})\n"," if bot_msg is not None:\n"," messages.append({\"role\": \"assistant\", \"content\": bot_msg})\n","\n"," # Add latest user message\n"," messages.append({\"role\": \"user\", \"content\": message})\n","\n"," # Call Groq chat completion\n"," response = client.chat.completions.create(\n"," model=MODEL_NAME,\n"," messages=messages,\n"," temperature=0.7,\n"," max_tokens=512,\n"," )\n","\n"," reply = response.choices[0].message.content\n"," return reply\n","\n","\n","# ---------- GRADIO UI ----------\n","with gr.Blocks() as demo:\n"," gr.Markdown(\"# πŸ’¬ Groq Llama Chatbot\")\n"," gr.Markdown(\n"," \"Chat with a Llama model served through the Groq API. \"\n"," \"Make sure you set your `GROQ_API_KEY` before running.\"\n"," )\n","\n"," chatbot = gr.Chatbot(height=400)\n"," msg = gr.Textbox(label=\"Type your message here\")\n"," clear = gr.Button(\"Clear\")\n","\n"," def user_send(user_message, chat_history):\n"," # Append user message to history; bot response handled by second fn\n"," chat_history = chat_history + [[user_message, None]]\n"," return \"\", chat_history\n","\n"," def bot_reply(chat_history):\n"," user_message = chat_history[-1][0]\n"," bot_answer = chat_with_groq(user_message, chat_history[:-1])\n"," chat_history[-1][1] = bot_answer\n"," return chat_history\n","\n"," msg.submit(user_send, [msg, chatbot], [msg, chatbot]).then(\n"," bot_reply, [chatbot], [chatbot]\n"," )\n"," clear.click(lambda: None, None, chatbot, queue=False)\n","\n","# launch app\n","if __name__ == \"__main__\":\n"," demo.launch()\n"]},{"cell_type":"code","source":[],"metadata":{"id":"fEn3E79aX85e"},"execution_count":null,"outputs":[]}]}
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+