Spaces:
Sleeping
Sleeping
feat(DOCUMENTATION): :bookmark: Add Jupyter notebook for API interaction, including functions for text generation and summarization with example usage.
Browse files- test.ipynb +154 -0
test.ipynb
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import requests"
|
10 |
+
]
|
11 |
+
},
|
12 |
+
{
|
13 |
+
"cell_type": "code",
|
14 |
+
"execution_count": 3,
|
15 |
+
"metadata": {},
|
16 |
+
"outputs": [],
|
17 |
+
"source": [
|
18 |
+
"# API configuration\n",
|
19 |
+
"API_URL = \"https://maximofn-iriusrisktestchallenge.hf.space\"\n",
|
20 |
+
"API_KEY = \"your_api_key\""
|
21 |
+
]
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"cell_type": "code",
|
25 |
+
"execution_count": 4,
|
26 |
+
"metadata": {},
|
27 |
+
"outputs": [],
|
28 |
+
"source": [
|
29 |
+
"# Headers for authentication\n",
|
30 |
+
"headers = {\n",
|
31 |
+
" \"X-API-Key\": API_KEY,\n",
|
32 |
+
" \"Content-Type\": \"application/json\"\n",
|
33 |
+
"}"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 5,
|
39 |
+
"metadata": {},
|
40 |
+
"outputs": [],
|
41 |
+
"source": [
|
42 |
+
"# Generate text\n",
|
43 |
+
"def generate_text(query, thread_id=\"default\", system_prompt=None):\n",
|
44 |
+
" url = f\"{API_URL}/generate\"\n",
|
45 |
+
" \n",
|
46 |
+
" data = {\n",
|
47 |
+
" \"query\": query,\n",
|
48 |
+
" \"thread_id\": thread_id\n",
|
49 |
+
" }\n",
|
50 |
+
" \n",
|
51 |
+
" # Add system prompt if provided\n",
|
52 |
+
" if system_prompt:\n",
|
53 |
+
" data[\"system_prompt\"] = system_prompt\n",
|
54 |
+
" \n",
|
55 |
+
" try:\n",
|
56 |
+
" response = requests.post(url, json=data, headers=headers)\n",
|
57 |
+
" if response.status_code == 200:\n",
|
58 |
+
" result = response.json()\n",
|
59 |
+
" return result[\"generated_text\"]\n",
|
60 |
+
" else:\n",
|
61 |
+
" print(f\"Error: {response.status_code}\")\n",
|
62 |
+
" print(f\"Details: {response.text}\")\n",
|
63 |
+
" return None\n",
|
64 |
+
" except Exception as e:\n",
|
65 |
+
" print(f\"Request failed: {str(e)}\")\n",
|
66 |
+
" return None"
|
67 |
+
]
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"cell_type": "code",
|
71 |
+
"execution_count": null,
|
72 |
+
"metadata": {},
|
73 |
+
"outputs": [],
|
74 |
+
"source": [
|
75 |
+
"# Example with custom thread and system prompt\n",
|
76 |
+
"result = generate_text(\n",
|
77 |
+
" query=\"Explain object-oriented programming\",\n",
|
78 |
+
" thread_id=\"programming_tutorial\",\n",
|
79 |
+
" system_prompt=\"You are a programming teacher. Explain concepts in simple terms.\"\n",
|
80 |
+
")"
|
81 |
+
]
|
82 |
+
},
|
83 |
+
{
|
84 |
+
"cell_type": "code",
|
85 |
+
"execution_count": 6,
|
86 |
+
"metadata": {},
|
87 |
+
"outputs": [],
|
88 |
+
"source": [
|
89 |
+
"# Summarize text\n",
|
90 |
+
"def summarize_text(text, max_length=200, thread_id=\"default\"):\n",
|
91 |
+
" url = f\"{API_URL}/summarize\"\n",
|
92 |
+
" \n",
|
93 |
+
" data = {\n",
|
94 |
+
" \"text\": text,\n",
|
95 |
+
" \"max_length\": max_length,\n",
|
96 |
+
" \"thread_id\": thread_id\n",
|
97 |
+
" }\n",
|
98 |
+
" \n",
|
99 |
+
" try:\n",
|
100 |
+
" response = requests.post(url, json=data, headers=headers)\n",
|
101 |
+
" if response.status_code == 200:\n",
|
102 |
+
" result = response.json()\n",
|
103 |
+
" return result[\"summary\"]\n",
|
104 |
+
" else:\n",
|
105 |
+
" print(f\"Error: {response.status_code}\")\n",
|
106 |
+
" print(f\"Details: {response.text}\")\n",
|
107 |
+
" return None\n",
|
108 |
+
" except Exception as e:\n",
|
109 |
+
" print(f\"Request failed: {str(e)}\")\n",
|
110 |
+
" return None"
|
111 |
+
]
|
112 |
+
},
|
113 |
+
{
|
114 |
+
"cell_type": "code",
|
115 |
+
"execution_count": null,
|
116 |
+
"metadata": {},
|
117 |
+
"outputs": [],
|
118 |
+
"source": [
|
119 |
+
"# Example usage\n",
|
120 |
+
"text_to_summarize = \"\"\"\n",
|
121 |
+
"Python is a high-level, interpreted programming language created by Guido van Rossum \n",
|
122 |
+
"and released in 1991. Python's design philosophy emphasizes code readability with \n",
|
123 |
+
"the use of significant whitespace. Its language constructs and object-oriented \n",
|
124 |
+
"approach aim to help programmers write clear, logical code for small and large-scale projects.\n",
|
125 |
+
"\"\"\"\n",
|
126 |
+
"\n",
|
127 |
+
"summary = summarize_text(text_to_summarize, max_length=50)\n",
|
128 |
+
"if summary:\n",
|
129 |
+
" print(\"Summary:\", summary)"
|
130 |
+
]
|
131 |
+
}
|
132 |
+
],
|
133 |
+
"metadata": {
|
134 |
+
"kernelspec": {
|
135 |
+
"display_name": "base",
|
136 |
+
"language": "python",
|
137 |
+
"name": "python3"
|
138 |
+
},
|
139 |
+
"language_info": {
|
140 |
+
"codemirror_mode": {
|
141 |
+
"name": "ipython",
|
142 |
+
"version": 3
|
143 |
+
},
|
144 |
+
"file_extension": ".py",
|
145 |
+
"mimetype": "text/x-python",
|
146 |
+
"name": "python",
|
147 |
+
"nbconvert_exporter": "python",
|
148 |
+
"pygments_lexer": "ipython3",
|
149 |
+
"version": "3.12.8"
|
150 |
+
}
|
151 |
+
},
|
152 |
+
"nbformat": 4,
|
153 |
+
"nbformat_minor": 2
|
154 |
+
}
|