Afeezee commited on
Commit
3a2c45d
·
verified ·
1 Parent(s): 665fb0c

Upload TriviaVilla (1).ipynb

Browse files
Files changed (1) hide show
  1. TriviaVilla (1).ipynb +539 -0
TriviaVilla (1).ipynb ADDED
@@ -0,0 +1,539 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "source": [
20
+ "pip install groq"
21
+ ],
22
+ "metadata": {
23
+ "id": "WVeN8pltcw-Z",
24
+ "colab": {
25
+ "base_uri": "https://localhost:8080/"
26
+ },
27
+ "outputId": "27da5ac4-bb36-4098-a9f5-8b85b8b9c0f9"
28
+ },
29
+ "execution_count": 36,
30
+ "outputs": [
31
+ {
32
+ "output_type": "stream",
33
+ "name": "stdout",
34
+ "text": [
35
+ "Requirement already satisfied: groq in /usr/local/lib/python3.10/dist-packages (0.9.0)\n",
36
+ "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.10/dist-packages (from groq) (3.7.1)\n",
37
+ "Requirement already satisfied: distro<2,>=1.7.0 in /usr/lib/python3/dist-packages (from groq) (1.7.0)\n",
38
+ "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.10/dist-packages (from groq) (0.27.0)\n",
39
+ "Requirement already satisfied: pydantic<3,>=1.9.0 in /usr/local/lib/python3.10/dist-packages (from groq) (2.8.2)\n",
40
+ "Requirement already satisfied: sniffio in /usr/local/lib/python3.10/dist-packages (from groq) (1.3.1)\n",
41
+ "Requirement already satisfied: typing-extensions<5,>=4.7 in /usr/local/lib/python3.10/dist-packages (from groq) (4.12.2)\n",
42
+ "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->groq) (3.7)\n",
43
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5,>=3.5.0->groq) (1.2.2)\n",
44
+ "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->groq) (2024.7.4)\n",
45
+ "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx<1,>=0.23.0->groq) (1.0.5)\n",
46
+ "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->groq) (0.14.0)\n",
47
+ "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->groq) (0.7.0)\n",
48
+ "Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic<3,>=1.9.0->groq) (2.20.1)\n"
49
+ ]
50
+ }
51
+ ]
52
+ },
53
+ {
54
+ "cell_type": "code",
55
+ "source": [
56
+ "pip install gradio"
57
+ ],
58
+ "metadata": {
59
+ "colab": {
60
+ "base_uri": "https://localhost:8080/"
61
+ },
62
+ "id": "wo0uk8aSC7Fy",
63
+ "outputId": "41d54a6f-9f95-4e61-e0ec-2469fbdba511"
64
+ },
65
+ "execution_count": 37,
66
+ "outputs": [
67
+ {
68
+ "output_type": "stream",
69
+ "name": "stdout",
70
+ "text": [
71
+ "Requirement already satisfied: gradio in /usr/local/lib/python3.10/dist-packages (4.39.0)\n",
72
+ "Requirement already satisfied: aiofiles<24.0,>=22.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (23.2.1)\n",
73
+ "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",
74
+ "Requirement already satisfied: fastapi in /usr/local/lib/python3.10/dist-packages (from gradio) (0.111.1)\n",
75
+ "Requirement already satisfied: ffmpy in /usr/local/lib/python3.10/dist-packages (from gradio) (0.3.2)\n",
76
+ "Requirement already satisfied: gradio-client==1.1.1 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.1.1)\n",
77
+ "Requirement already satisfied: httpx>=0.24.1 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.27.0)\n",
78
+ "Requirement already satisfied: huggingface-hub>=0.19.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.23.5)\n",
79
+ "Requirement already satisfied: importlib-resources<7.0,>=1.3 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.4.0)\n",
80
+ "Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.1.4)\n",
81
+ "Requirement already satisfied: markupsafe~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.1.5)\n",
82
+ "Requirement already satisfied: matplotlib~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.7.1)\n",
83
+ "Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (1.25.2)\n",
84
+ "Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (3.10.6)\n",
85
+ "Requirement already satisfied: packaging in /usr/local/lib/python3.10/dist-packages (from gradio) (24.1)\n",
86
+ "Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.3)\n",
87
+ "Requirement already satisfied: pillow<11.0,>=8.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (9.4.0)\n",
88
+ "Requirement already satisfied: pydantic>=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.8.2)\n",
89
+ "Requirement already satisfied: pydub in /usr/local/lib/python3.10/dist-packages (from gradio) (0.25.1)\n",
90
+ "Requirement already satisfied: python-multipart>=0.0.9 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.0.9)\n",
91
+ "Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (6.0.1)\n",
92
+ "Requirement already satisfied: ruff>=0.2.2 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.5.4)\n",
93
+ "Requirement already satisfied: semantic-version~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.10.0)\n",
94
+ "Requirement already satisfied: tomlkit==0.12.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.12.0)\n",
95
+ "Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.12.3)\n",
96
+ "Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (4.12.2)\n",
97
+ "Requirement already satisfied: urllib3~=2.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (2.0.7)\n",
98
+ "Requirement already satisfied: uvicorn>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from gradio) (0.30.3)\n",
99
+ "Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.1.1->gradio) (2023.6.0)\n",
100
+ "Requirement already satisfied: websockets<12.0,>=10.0 in /usr/local/lib/python3.10/dist-packages (from gradio-client==1.1.1->gradio) (11.0.3)\n",
101
+ "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio) (3.7)\n",
102
+ "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n",
103
+ "Requirement already satisfied: exceptiongroup in /usr/local/lib/python3.10/dist-packages (from anyio<5.0,>=3.0->gradio) (1.2.2)\n",
104
+ "Requirement already satisfied: certifi in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (2024.7.4)\n",
105
+ "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.10/dist-packages (from httpx>=0.24.1->gradio) (1.0.5)\n",
106
+ "Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.10/dist-packages (from httpcore==1.*->httpx>=0.24.1->gradio) (0.14.0)\n",
107
+ "Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (3.15.4)\n",
108
+ "Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (2.31.0)\n",
109
+ "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub>=0.19.3->gradio) (4.66.4)\n",
110
+ "Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.2.1)\n",
111
+ "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (0.12.1)\n",
112
+ "Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (4.53.1)\n",
113
+ "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (1.4.5)\n",
114
+ "Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (3.1.2)\n",
115
+ "Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib~=3.0->gradio) (2.8.2)\n",
116
+ "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2023.4)\n",
117
+ "Requirement already satisfied: tzdata>=2022.1 in /usr/local/lib/python3.10/dist-packages (from pandas<3.0,>=1.0->gradio) (2024.1)\n",
118
+ "Requirement already satisfied: annotated-types>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (0.7.0)\n",
119
+ "Requirement already satisfied: pydantic-core==2.20.1 in /usr/local/lib/python3.10/dist-packages (from pydantic>=2.0->gradio) (2.20.1)\n",
120
+ "Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (8.1.7)\n",
121
+ "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
122
+ "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.10/dist-packages (from typer<1.0,>=0.12->gradio) (13.7.1)\n",
123
+ "Requirement already satisfied: starlette<0.38.0,>=0.37.2 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (0.37.2)\n",
124
+ "Requirement already satisfied: fastapi-cli>=0.0.2 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (0.0.4)\n",
125
+ "Requirement already satisfied: email_validator>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from fastapi->gradio) (2.2.0)\n",
126
+ "Requirement already satisfied: dnspython>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from email_validator>=2.0.0->fastapi->gradio) (2.6.1)\n",
127
+ "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib~=3.0->gradio) (1.16.0)\n",
128
+ "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n",
129
+ "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.16.1)\n",
130
+ "Requirement already satisfied: httptools>=0.5.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn[standard]>=0.12.0->fastapi->gradio) (0.6.1)\n",
131
+ "Requirement already satisfied: python-dotenv>=0.13 in /usr/local/lib/python3.10/dist-packages (from uvicorn[standard]>=0.12.0->fastapi->gradio) (1.0.1)\n",
132
+ "Requirement already satisfied: uvloop!=0.15.0,!=0.15.1,>=0.14.0 in /usr/local/lib/python3.10/dist-packages (from uvicorn[standard]>=0.12.0->fastapi->gradio) (0.19.0)\n",
133
+ "Requirement already satisfied: watchfiles>=0.13 in /usr/local/lib/python3.10/dist-packages (from uvicorn[standard]>=0.12.0->fastapi->gradio) (0.22.0)\n",
134
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->huggingface-hub>=0.19.3->gradio) (3.3.2)\n",
135
+ "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n"
136
+ ]
137
+ }
138
+ ]
139
+ },
140
+ {
141
+ "cell_type": "code",
142
+ "execution_count": 41,
143
+ "metadata": {
144
+ "colab": {
145
+ "base_uri": "https://localhost:8080/",
146
+ "height": 646
147
+ },
148
+ "id": "zeBQLQB_COtN",
149
+ "outputId": "79852c24-fe4a-447d-8923-3748694f35d9"
150
+ },
151
+ "outputs": [
152
+ {
153
+ "output_type": "stream",
154
+ "name": "stdout",
155
+ "text": [
156
+ "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
157
+ "\n",
158
+ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
159
+ "Running on public URL: https://5f879f53ecb6335748.gradio.live\n",
160
+ "\n",
161
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
162
+ ]
163
+ },
164
+ {
165
+ "output_type": "display_data",
166
+ "data": {
167
+ "text/plain": [
168
+ "<IPython.core.display.HTML object>"
169
+ ],
170
+ "text/html": [
171
+ "<div><iframe src=\"https://5f879f53ecb6335748.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
172
+ ]
173
+ },
174
+ "metadata": {}
175
+ },
176
+ {
177
+ "output_type": "execute_result",
178
+ "data": {
179
+ "text/plain": []
180
+ },
181
+ "metadata": {},
182
+ "execution_count": 41
183
+ }
184
+ ],
185
+ "source": [
186
+ "from groq import Groq\n",
187
+ "import gradio as gr\n",
188
+ "\n",
189
+ "# Initialize the Groq client with your API key\n",
190
+ "client = Groq(\n",
191
+ " api_key=\"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\"\n",
192
+ ")\n",
193
+ "\n",
194
+ "# List to maintain the conversation history, starting with a default prompt\n",
195
+ "conversation_history = [\n",
196
+ " {\"role\": \"system\", \"content\": \"You are an assistant in a trivia game focused on Nigerian music.\"}\n",
197
+ "]\n",
198
+ "\n",
199
+ "# Function to get the initial LLM output and start the conversation\n",
200
+ "def start_trivia_game():\n",
201
+ " # Initial message to start the game\n",
202
+ " initial_message = \"Start a Trivia on Nigeria music. Assess the answer and provide the percentage score\"\n",
203
+ "\n",
204
+ " # Add the initial message to the conversation history\n",
205
+ " conversation_history.append({\"role\": \"user\", \"content\": initial_message})\n",
206
+ "\n",
207
+ " # Get completion from the LLM for the initial question\n",
208
+ " completion = client.chat.completions.create(\n",
209
+ " model=\"llama-3.1-70b-versatile\",\n",
210
+ " messages=conversation_history,\n",
211
+ " temperature=1,\n",
212
+ " max_tokens=1024,\n",
213
+ " top_p=1,\n",
214
+ " stream=True,\n",
215
+ " stop=None,\n",
216
+ " )\n",
217
+ "\n",
218
+ " llm_output = \"\"\n",
219
+ " for chunk in completion:\n",
220
+ " llm_output += chunk.choices[0].delta.content or \"\"\n",
221
+ "\n",
222
+ " # Add the assistant's response to the conversation history\n",
223
+ " conversation_history.append({\"role\": \"assistant\", \"content\": llm_output})\n",
224
+ "\n",
225
+ " return llm_output\n",
226
+ "\n",
227
+ "# Function to handle user response and continue the conversation\n",
228
+ "def continue_trivia_game(user_response):\n",
229
+ " # Add user's response to the conversation history\n",
230
+ " conversation_history.append({\"role\": \"user\", \"content\": user_response})\n",
231
+ "\n",
232
+ " # Get completion from the LLM for the user's response\n",
233
+ " completion = client.chat.completions.create(\n",
234
+ " model=\"llama-3.1-70b-versatile\",\n",
235
+ " messages=conversation_history,\n",
236
+ " temperature=1,\n",
237
+ " max_tokens=1024,\n",
238
+ " top_p=1,\n",
239
+ " stream=True,\n",
240
+ " stop=None,\n",
241
+ " )\n",
242
+ "\n",
243
+ " llm_output = \"\"\n",
244
+ " for chunk in completion:\n",
245
+ " llm_output += chunk.choices[0].delta.content or \"\"\n",
246
+ "\n",
247
+ " # Add the assistant's response to the conversation history\n",
248
+ " conversation_history.append({\"role\": \"assistant\", \"content\": llm_output})\n",
249
+ "\n",
250
+ " return llm_output\n",
251
+ "\n",
252
+ "# Start the game and get the initial LLM output\n",
253
+ "initial_output = start_trivia_game()\n",
254
+ "\n",
255
+ "# Using gr.Blocks to create the interface\n",
256
+ "with gr.Blocks() as demo:\n",
257
+ " # Title and Description\n",
258
+ " gr.Markdown(\"# Trivia Game\\nA simple trivia game using Groq's LLama 3 LLM API.\")\n",
259
+ "\n",
260
+ " # LLM Output Textbox\n",
261
+ " llm_output = gr.Textbox(label=\"LLM Output\", placeholder=\"The output from the LLM will appear here\", lines=10, value=initial_output)\n",
262
+ "\n",
263
+ " # User Response Textbox\n",
264
+ " user_response = gr.Textbox(label=\"Your Response\", placeholder=\"Type your response here\", lines=3)\n",
265
+ "\n",
266
+ " # Button to submit the user's response and update the LLM output\n",
267
+ " submit_button = gr.Button(\"Submit\")\n",
268
+ "\n",
269
+ " # Function to update the LLM output upon submission\n",
270
+ " def update_llm_output(user_input):\n",
271
+ " # Update the LLM output and return it\n",
272
+ " return continue_trivia_game(user_input)\n",
273
+ "\n",
274
+ " # Define interactions\n",
275
+ " submit_button.click(fn=update_llm_output, inputs=user_response, outputs=llm_output)\n",
276
+ "\n",
277
+ "# Launch the Gradio app\n",
278
+ "demo.launch()\n",
279
+ "\n"
280
+ ]
281
+ },
282
+ {
283
+ "cell_type": "code",
284
+ "source": [
285
+ "from groq import Groq\n",
286
+ "import gradio as gr\n",
287
+ "\n",
288
+ "# Initialize the Groq client with your API key\n",
289
+ "client = Groq(\n",
290
+ " api_key=\"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\"\n",
291
+ ")\n",
292
+ "\n",
293
+ "# List to maintain the conversation history\n",
294
+ "conversation_history = [\n",
295
+ " {\"role\": \"system\", \"content\": \"You are an assistant in a trivia game focused on Nigerian music.\"}\n",
296
+ "]\n",
297
+ "\n",
298
+ "# Function to get the LLM output and update the conversation history\n",
299
+ "def trivia_game(user_response):\n",
300
+ " # Add user's response to the conversation history\n",
301
+ " conversation_history.append({\"role\": \"user\", \"content\": user_response})\n",
302
+ "\n",
303
+ " # Get completion from the LLM\n",
304
+ " completion = client.chat.completions.create(\n",
305
+ " model=\"llama-3.1-70b-versatile\",\n",
306
+ " messages=conversation_history,\n",
307
+ " temperature=1,\n",
308
+ " max_tokens=1024,\n",
309
+ " top_p=1,\n",
310
+ " stream=True,\n",
311
+ " stop=None,\n",
312
+ " )\n",
313
+ "\n",
314
+ " llm_output = \"\"\n",
315
+ " for chunk in completion:\n",
316
+ " llm_output += chunk.choices[0].delta.content or \"\"\n",
317
+ "\n",
318
+ " # Add the assistant's response to the conversation history\n",
319
+ " conversation_history.append({\"role\": \"assistant\", \"content\": llm_output})\n",
320
+ "\n",
321
+ " return llm_output\n",
322
+ "\n",
323
+ "# Creating Gradio Interface\n",
324
+ "llm_output = gr.Textbox(label=\"LLM Output\", placeholder=\"The output from the LLM will appear here\", lines=10)\n",
325
+ "user_response = gr.Textbox(label=\"Your Response\", placeholder=\"Type your response here\", lines=3)\n",
326
+ "\n",
327
+ "demo = gr.Interface(\n",
328
+ " fn=trivia_game,\n",
329
+ " inputs=user_response,\n",
330
+ " outputs=llm_output,\n",
331
+ " title=\"Trivia Game\",\n",
332
+ " description=\"A simple trivia game using Groq's LLama 3 LLM API\"\n",
333
+ ")\n",
334
+ "\n",
335
+ "# Starting the Gradio app\n",
336
+ "demo.launch()\n",
337
+ "\n"
338
+ ],
339
+ "metadata": {
340
+ "colab": {
341
+ "base_uri": "https://localhost:8080/",
342
+ "height": 646
343
+ },
344
+ "id": "M9Uz7BJ6NOkO",
345
+ "outputId": "6aa70fee-3bf7-46b7-e232-393e8352e5af"
346
+ },
347
+ "execution_count": null,
348
+ "outputs": [
349
+ {
350
+ "output_type": "stream",
351
+ "name": "stdout",
352
+ "text": [
353
+ "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
354
+ "\n",
355
+ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
356
+ "Running on public URL: https://f24a7676e50f0183bd.gradio.live\n",
357
+ "\n",
358
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
359
+ ]
360
+ },
361
+ {
362
+ "output_type": "display_data",
363
+ "data": {
364
+ "text/plain": [
365
+ "<IPython.core.display.HTML object>"
366
+ ],
367
+ "text/html": [
368
+ "<div><iframe src=\"https://f24a7676e50f0183bd.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
369
+ ]
370
+ },
371
+ "metadata": {}
372
+ },
373
+ {
374
+ "output_type": "execute_result",
375
+ "data": {
376
+ "text/plain": []
377
+ },
378
+ "metadata": {},
379
+ "execution_count": 33
380
+ }
381
+ ]
382
+ },
383
+ {
384
+ "cell_type": "code",
385
+ "source": [
386
+ "from groq import Groq\n",
387
+ "import gradio as gr\n",
388
+ "\n",
389
+ "# Initialize the Groq client with your API key\n",
390
+ "client = Groq(\n",
391
+ " api_key=\"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\"\n",
392
+ ")\n",
393
+ "\n",
394
+ "# List to maintain the conversation history, starting with a default prompt\n",
395
+ "conversation_history = [\n",
396
+ " {\"role\": \"system\", \"content\": \"You are an assistant in a trivia game focused on Nigerian music.\"}\n",
397
+ "]\n",
398
+ "\n",
399
+ "# Function to get the initial LLM output and start the conversation\n",
400
+ "def start_trivia_game():\n",
401
+ " # Initial message to start the game\n",
402
+ " initial_message = \"Start a Trivia on Nigeria music. Assess the answer and provide the percentage score\"\n",
403
+ "\n",
404
+ " # Add the initial message to the conversation history\n",
405
+ " conversation_history.append({\"role\": \"user\", \"content\": initial_message})\n",
406
+ "\n",
407
+ " # Get completion from the LLM for the initial question\n",
408
+ " completion = client.chat.completions.create(\n",
409
+ " model=\"llama-3.1-70b-versatile\",\n",
410
+ " messages=conversation_history,\n",
411
+ " temperature=1,\n",
412
+ " max_tokens=1024,\n",
413
+ " top_p=1,\n",
414
+ " stream=True,\n",
415
+ " stop=None,\n",
416
+ " )\n",
417
+ "\n",
418
+ " llm_output = \"\"\n",
419
+ " for chunk in completion:\n",
420
+ " llm_output += chunk.choices[0].delta.content or \"\"\n",
421
+ "\n",
422
+ " # Add the assistant's response to the conversation history\n",
423
+ " conversation_history.append({\"role\": \"assistant\", \"content\": llm_output})\n",
424
+ "\n",
425
+ " return llm_output\n",
426
+ "\n",
427
+ "# Function to handle user response and continue the conversation\n",
428
+ "def continue_trivia_game(user_response):\n",
429
+ " # Add user's response to the conversation history\n",
430
+ " conversation_history.append({\"role\": \"user\", \"content\": user_response})\n",
431
+ "\n",
432
+ " # Get completion from the LLM for the user's response\n",
433
+ " completion = client.chat.completions.create(\n",
434
+ " model=\"llama-3.1-70b-versatile\",\n",
435
+ " messages=conversation_history,\n",
436
+ " temperature=1,\n",
437
+ " max_tokens=1024,\n",
438
+ " top_p=1,\n",
439
+ " stream=True,\n",
440
+ " stop=None,\n",
441
+ " )\n",
442
+ "\n",
443
+ " llm_output = \"\"\n",
444
+ " for chunk in completion:\n",
445
+ " llm_output += chunk.choices[0].delta.content or \"\"\n",
446
+ "\n",
447
+ " # Add the assistant's response to the conversation history\n",
448
+ " conversation_history.append({\"role\": \"assistant\", \"content\": llm_output})\n",
449
+ "\n",
450
+ " return llm_output\n",
451
+ "\n",
452
+ "# Start the game and get the initial LLM output\n",
453
+ "initial_output = start_trivia_game()\n",
454
+ "\n",
455
+ "# Creating Gradio Interface\n",
456
+ "llm_output = gr.Textbox(label=\"LLM Output\", placeholder=\"The output from the LLM will appear here\", lines=10, value=initial_output)\n",
457
+ "user_response = gr.Textbox(label=\"Your Response\", placeholder=\"Type your response here\", lines=3)\n",
458
+ "\n",
459
+ "demo = gr.Interface(\n",
460
+ " fn=continue_trivia_game,\n",
461
+ " inputs=user_response,\n",
462
+ " outputs=llm_output,\n",
463
+ " title=\"TriviaVilla--Nigerian Music Trivia Game\",\n",
464
+ " description=\"How much do you know Nigerian Music? Here is a simple trivia game on Nigerian Music using LLama 3.1\",\n",
465
+ ")\n",
466
+ "\n",
467
+ "# Starting the Gradio app\n",
468
+ "demo.launch()\n",
469
+ "\n"
470
+ ],
471
+ "metadata": {
472
+ "colab": {
473
+ "base_uri": "https://localhost:8080/",
474
+ "height": 646
475
+ },
476
+ "id": "dUpGIo3eHT6t",
477
+ "outputId": "a56d0ca1-43ad-4092-d756-24b01ec9ae58"
478
+ },
479
+ "execution_count": null,
480
+ "outputs": [
481
+ {
482
+ "output_type": "stream",
483
+ "name": "stdout",
484
+ "text": [
485
+ "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
486
+ "\n",
487
+ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
488
+ "Running on public URL: https://0d7874273562a8d25f.gradio.live\n",
489
+ "\n",
490
+ "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n"
491
+ ]
492
+ },
493
+ {
494
+ "output_type": "display_data",
495
+ "data": {
496
+ "text/plain": [
497
+ "<IPython.core.display.HTML object>"
498
+ ],
499
+ "text/html": [
500
+ "<div><iframe src=\"https://0d7874273562a8d25f.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
501
+ ]
502
+ },
503
+ "metadata": {}
504
+ },
505
+ {
506
+ "output_type": "execute_result",
507
+ "data": {
508
+ "text/plain": []
509
+ },
510
+ "metadata": {},
511
+ "execution_count": 35
512
+ }
513
+ ]
514
+ },
515
+ {
516
+ "cell_type": "code",
517
+ "source": [],
518
+ "metadata": {
519
+ "colab": {
520
+ "base_uri": "https://localhost:8080/",
521
+ "height": 106
522
+ },
523
+ "id": "U1vMYZkFDYgh",
524
+ "outputId": "66ef485d-d9c8-4949-91da-f4620b3a03da"
525
+ },
526
+ "execution_count": null,
527
+ "outputs": [
528
+ {
529
+ "output_type": "error",
530
+ "ename": "SyntaxError",
531
+ "evalue": "invalid syntax (<ipython-input-6-45df297f8215>, line 1)",
532
+ "traceback": [
533
+ "\u001b[0;36m File \u001b[0;32m\"<ipython-input-6-45df297f8215>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m export Groq_API_KEY= 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\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
534
+ ]
535
+ }
536
+ ]
537
+ }
538
+ ]
539
+ }