dlynch90 commited on
Commit
34b6d53
·
verified ·
1 Parent(s): 725f4fc

Create EXAMPLES.md

Browse files
Files changed (1) hide show
  1. EXAMPLES.md +366 -0
EXAMPLES.md ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # AI Agency Pro - Usage Examples
2
+
3
+ Real-world usage patterns demonstrating vendor-driven development with HuggingFace libraries.
4
+
5
+ ---
6
+
7
+ ## Quick Start
8
+
9
+ ### Using the Web Interface
10
+
11
+ 1. Navigate to the [AI Agency Pro Space](https://huggingface.co/spaces/dlynch90/AI-Agency-Pro)
12
+ 2. Select a tab (Summarizer, Classifier, Q&A Agent, or Chat Agent)
13
+ 3. Enter your input and click the action button
14
+ 4. View results instantly with GPU acceleration
15
+
16
+ ---
17
+
18
+ ## Python API Examples
19
+
20
+ ### Example 1: Text Summarization
21
+
22
+ ```python
23
+ from huggingface_hub import InferenceClient
24
+
25
+ # Initialize client (official vendor pattern)
26
+ client = InferenceClient()
27
+
28
+ # Summarize text using official model
29
+ text = """
30
+ Artificial intelligence has transformed how businesses operate.
31
+ From automating customer service to optimizing supply chains,
32
+ AI technologies are driving unprecedented efficiency gains.
33
+ Companies investing in AI report 40% productivity improvements.
34
+ """
35
+
36
+ result = client.summarization(
37
+ text,
38
+ model="facebook/bart-large-cnn",
39
+ parameters={"max_length": 150, "min_length": 30}
40
+ )
41
+ print(result)
42
+ ```
43
+
44
+ ### Example 2: Zero-Shot Classification
45
+
46
+ ```python
47
+ from huggingface_hub import InferenceClient
48
+
49
+ client = InferenceClient()
50
+
51
+ # Classify text without training
52
+ text = "I just bought the new iPhone and it's amazing!"
53
+ labels = ["technology", "sports", "politics", "entertainment"]
54
+
55
+ result = client.zero_shot_classification(
56
+ text,
57
+ labels,
58
+ model="facebook/bart-large-mnli"
59
+ )
60
+
61
+ for label, score in zip(result["labels"], result["scores"]):
62
+ print(f"{label}: {score:.2%}")
63
+ ```
64
+
65
+ ### Example 3: Question Answering
66
+
67
+ ```python
68
+ from huggingface_hub import InferenceClient
69
+
70
+ client = InferenceClient()
71
+
72
+ context = """
73
+ Hugging Face was founded in 2016 by Clement Delangue,
74
+ Julien Chaumond, and Thomas Wolf. The company is
75
+ headquartered in New York City and has raised over
76
+ $160 million in funding.
77
+ """
78
+
79
+ question = "When was Hugging Face founded?"
80
+
81
+ result = client.question_answering(
82
+ question=question,
83
+ context=context,
84
+ model="deepset/roberta-base-squad2"
85
+ )
86
+
87
+ print(f"Answer: {result['answer']}")
88
+ print(f"Confidence: {result['score']:.2%}")
89
+ ```
90
+
91
+ ### Example 4: Chat with LLM
92
+
93
+ ```python
94
+ from huggingface_hub import InferenceClient
95
+
96
+ client = InferenceClient()
97
+
98
+ messages = [
99
+ {"role": "user", "content": "Explain quantum computing in simple terms."}
100
+ ]
101
+
102
+ response = client.chat_completion(
103
+ messages,
104
+ model="mistralai/Mistral-7B-Instruct-v0.3",
105
+ max_tokens=500
106
+ )
107
+
108
+ print(response.choices[0].message.content)
109
+ ```
110
+
111
+ ### Example 5: Streaming Chat Response
112
+
113
+ ```python
114
+ from huggingface_hub import InferenceClient
115
+
116
+ client = InferenceClient()
117
+
118
+ messages = [{"role": "user", "content": "Write a haiku about AI."}]
119
+
120
+ # Stream response for real-time output
121
+ for token in client.chat_completion(
122
+ messages,
123
+ model="mistralai/Mistral-7B-Instruct-v0.3",
124
+ max_tokens=100,
125
+ stream=True
126
+ ):
127
+ print(token.choices[0].delta.content, end="")
128
+ ```
129
+
130
+ ---
131
+
132
+ ## Gradio Integration Examples
133
+
134
+ ### Example 6: Custom Summarization Interface
135
+
136
+ ```python
137
+ import gradio as gr
138
+ from huggingface_hub import InferenceClient
139
+
140
+ client = InferenceClient()
141
+
142
+ def summarize(text, max_length=150):
143
+ result = client.summarization(
144
+ text,
145
+ model="facebook/bart-large-cnn",
146
+ parameters={"max_length": max_length}
147
+ )
148
+ return result
149
+
150
+ iface = gr.Interface(
151
+ fn=summarize,
152
+ inputs=[
153
+ gr.Textbox(label="Text to Summarize", lines=10),
154
+ gr.Slider(50, 300, value=150, label="Max Length")
155
+ ],
156
+ outputs=gr.Textbox(label="Summary"),
157
+ title="Text Summarizer"
158
+ )
159
+
160
+ iface.launch()
161
+ ```
162
+
163
+ ### Example 7: Multi-Tab Application
164
+
165
+ ```python
166
+ import gradio as gr
167
+ from huggingface_hub import InferenceClient
168
+
169
+ client = InferenceClient()
170
+
171
+ def summarize(text):
172
+ return client.summarization(text, model="facebook/bart-large-cnn")
173
+
174
+ def classify(text, labels):
175
+ label_list = [l.strip() for l in labels.split(",")]
176
+ result = client.zero_shot_classification(text, label_list)
177
+ return {l: s for l, s in zip(result["labels"], result["scores"])}
178
+
179
+ with gr.Blocks() as demo:
180
+ gr.Markdown("# Multi-Agent System")
181
+
182
+ with gr.Tab("Summarizer"):
183
+ text_in = gr.Textbox(label="Input")
184
+ text_out = gr.Textbox(label="Summary")
185
+ gr.Button("Summarize").click(summarize, text_in, text_out)
186
+
187
+ with gr.Tab("Classifier"):
188
+ cls_text = gr.Textbox(label="Text")
189
+ cls_labels = gr.Textbox(label="Labels (comma-separated)")
190
+ cls_out = gr.Label(label="Results")
191
+ gr.Button("Classify").click(classify, [cls_text, cls_labels], cls_out)
192
+
193
+ demo.launch()
194
+ ```
195
+
196
+ ---
197
+
198
+ ## ZeroGPU Examples
199
+
200
+ ### Example 8: GPU-Accelerated Processing
201
+
202
+ ```python
203
+ import spaces
204
+ import gradio as gr
205
+ from huggingface_hub import InferenceClient
206
+
207
+ client = InferenceClient()
208
+
209
+ @spaces.GPU(duration=60) # Request GPU for 60 seconds
210
+ def heavy_processing(text):
211
+ """GPU-accelerated inference."""
212
+ # Long-running inference task
213
+ result = client.text_generation(
214
+ text,
215
+ model="mistralai/Mistral-7B-Instruct-v0.3",
216
+ max_new_tokens=1000
217
+ )
218
+ return result
219
+
220
+ iface = gr.Interface(
221
+ fn=heavy_processing,
222
+ inputs=gr.Textbox(label="Prompt"),
223
+ outputs=gr.Textbox(label="Generated Text")
224
+ )
225
+
226
+ iface.launch()
227
+ ```
228
+
229
+ ---
230
+
231
+ ## Batch Processing Examples
232
+
233
+ ### Example 9: Process Multiple Documents
234
+
235
+ ```python
236
+ from huggingface_hub import InferenceClient
237
+ import asyncio
238
+
239
+ client = InferenceClient()
240
+
241
+ async def batch_summarize(documents):
242
+ """Summarize multiple documents efficiently."""
243
+ results = []
244
+ for doc in documents:
245
+ summary = client.summarization(
246
+ doc,
247
+ model="facebook/bart-large-cnn"
248
+ )
249
+ results.append(summary)
250
+ return results
251
+
252
+ # Usage
253
+ documents = [
254
+ "First document text...",
255
+ "Second document text...",
256
+ "Third document text..."
257
+ ]
258
+
259
+ summaries = asyncio.run(batch_summarize(documents))
260
+ for i, summary in enumerate(summaries):
261
+ print(f"Document {i+1}: {summary}")
262
+ ```
263
+
264
+ ---
265
+
266
+ ## Error Handling Examples
267
+
268
+ ### Example 10: Robust API Calls
269
+
270
+ ```python
271
+ from huggingface_hub import InferenceClient
272
+ import logging
273
+
274
+ logging.basicConfig(level=logging.INFO)
275
+ logger = logging.getLogger(__name__)
276
+
277
+ client = InferenceClient()
278
+
279
+ def safe_summarize(text, max_retries=3):
280
+ """Summarize with error handling and retries."""
281
+ for attempt in range(max_retries):
282
+ try:
283
+ if not text or len(text.strip()) < 10:
284
+ return "Error: Text too short to summarize."
285
+
286
+ result = client.summarization(
287
+ text,
288
+ model="facebook/bart-large-cnn"
289
+ )
290
+ return result
291
+
292
+ except Exception as e:
293
+ logger.warning(f"Attempt {attempt + 1} failed: {e}")
294
+ if attempt == max_retries - 1:
295
+ return f"Error: {str(e)}"
296
+
297
+ return "Error: Max retries exceeded."
298
+ ```
299
+
300
+ ---
301
+
302
+ ## Integration Patterns
303
+
304
+ ### Example 11: FastAPI Integration
305
+
306
+ ```python
307
+ from fastapi import FastAPI
308
+ from huggingface_hub import InferenceClient
309
+ from pydantic import BaseModel
310
+
311
+ app = FastAPI()
312
+ client = InferenceClient()
313
+
314
+ class SummarizeRequest(BaseModel):
315
+ text: str
316
+ max_length: int = 150
317
+
318
+ @app.post("/summarize")
319
+ async def summarize(request: SummarizeRequest):
320
+ result = client.summarization(
321
+ request.text,
322
+ model="facebook/bart-large-cnn",
323
+ parameters={"max_length": request.max_length}
324
+ )
325
+ return {"summary": result}
326
+ ```
327
+
328
+ ### Example 12: Flask Integration
329
+
330
+ ```python
331
+ from flask import Flask, request, jsonify
332
+ from huggingface_hub import InferenceClient
333
+
334
+ app = Flask(__name__)
335
+ client = InferenceClient()
336
+
337
+ @app.route("/classify", methods=["POST"])
338
+ def classify():
339
+ data = request.json
340
+ result = client.zero_shot_classification(
341
+ data["text"],
342
+ data["labels"],
343
+ model="facebook/bart-large-mnli"
344
+ )
345
+ return jsonify(result)
346
+ ```
347
+
348
+ ---
349
+
350
+ ## Best Practices Demonstrated
351
+
352
+ 1. **Always use InferenceClient** - Official HuggingFace pattern
353
+ 2. **Implement error handling** - Graceful degradation
354
+ 3. **Use @spaces.GPU** - Efficient GPU allocation
355
+ 4. **Add logging** - Observability and debugging
356
+ 5. **Validate inputs** - Prevent API errors
357
+ 6. **Use official model IDs** - Reliability and updates
358
+
359
+ ---
360
+
361
+ ## References
362
+
363
+ - [HuggingFace Hub Python Library](https://huggingface.co/docs/huggingface_hub)
364
+ - [Gradio Documentation](https://gradio.app/docs)
365
+ - [Spaces ZeroGPU](https://huggingface.co/docs/hub/spaces-zerogpu)
366
+ - [Inference API](https://huggingface.co/docs/api-inference)