sunnysharma20 commited on
Commit
1dba3b8
·
verified ·
1 Parent(s): b961662

Upload 10thNov2024_assignment.ipynb

Browse files
Files changed (1) hide show
  1. 10thNov2024_assignment.ipynb +95 -0
10thNov2024_assignment.ipynb ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "import requests\n",
21
+ "import json\n",
22
+ "\n",
23
+ "# Replace with your actual API key\n",
24
+ "API_KEY = \"AIzaSyBIO30nsBvTSnv2ReRfH_mBEPc77MzvM40\"\n",
25
+ "\n",
26
+ "def chat(prompt):\n",
27
+ " \"\"\"\n",
28
+ " Sends a prompt to the Gemini API and returns the response.\n",
29
+ " \"\"\"\n",
30
+ " url = \"https://api.gemini.com/v1/chat/generate\"\n",
31
+ " headers = {\n",
32
+ " \"Authorization\": f\"Bearer {API_KEY}\",\n",
33
+ " \"Content-Type\": \"application/json\",\n",
34
+ " }\n",
35
+ "\n",
36
+ " data = {\n",
37
+ " \"prompt\": prompt,\n",
38
+ " }\n",
39
+ "\n",
40
+ " response = requests.post(url, headers=headers, json=data)\n",
41
+ "\n",
42
+ " if response.status_code == 200:\n",
43
+ " response_json = response.json()\n",
44
+ " return response_json[\"text\"]\n",
45
+ " else:\n",
46
+ " print(f\"Error: {response.status_code} - {response.text}\")\n",
47
+ " return \"An error occurred.\"\n",
48
+ "\n",
49
+ "if __name__ == \"__main__\":\n",
50
+ " while True:\n",
51
+ " user_input = input(\"You: \")\n",
52
+ " if user_input.lower() == \"exit\":\n",
53
+ " break\n",
54
+ " response = chat(user_input)\n",
55
+ " print(\"Gemini: \" + response)"
56
+ ],
57
+ "metadata": {
58
+ "colab": {
59
+ "base_uri": "https://localhost:8080/",
60
+ "height": 497
61
+ },
62
+ "id": "F2KwyO_bma9p",
63
+ "outputId": "f81d479c-57a9-4bb4-8ce6-4492f1376994"
64
+ },
65
+ "execution_count": 7,
66
+ "outputs": [
67
+ {
68
+ "output_type": "stream",
69
+ "name": "stdout",
70
+ "text": [
71
+ "You: hi\n",
72
+ "Error: 404 - {\"result\":\"error\",\"reason\":\"EndpointNotFound\",\"message\":\"API entry point `/v1/chat/generate` not found\"}\n",
73
+ "Gemini: An error occurred.\n",
74
+ "You: what\n",
75
+ "Error: 404 - {\"result\":\"error\",\"reason\":\"EndpointNotFound\",\"message\":\"API entry point `/v1/chat/generate` not found\"}\n",
76
+ "Gemini: An error occurred.\n"
77
+ ]
78
+ },
79
+ {
80
+ "output_type": "error",
81
+ "ename": "KeyboardInterrupt",
82
+ "evalue": "Interrupted by user",
83
+ "traceback": [
84
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
85
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
86
+ "\u001b[0;32m<ipython-input-7-e5f93e7139ac>\u001b[0m in \u001b[0;36m<cell line: 30>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 30\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"__main__\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 31\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 32\u001b[0;31m \u001b[0muser_input\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"You: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 33\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0muser_input\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlower\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m\"exit\"\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
87
+ "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 849\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 850\u001b[0m )\n\u001b[0;32m--> 851\u001b[0;31m return self._input_request(str(prompt),\n\u001b[0m\u001b[1;32m 852\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 853\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
88
+ "\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 893\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 894\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 895\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 896\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 897\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
89
+ "\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
90
+ ]
91
+ }
92
+ ]
93
+ }
94
+ ]
95
+ }