Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
print("\n#### Example: Deploying a Simple LLM with Gradio to Hugging Face Spaces")
|
| 7 |
+
print("The following code demonstrates a simple LLM inference application using Gradio, which can be easily deployed to Hugging Face Spaces. For this example, we'll use a small, efficient model.")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# --- Step 1: Load the LLM --- #
|
| 11 |
+
# We'll use a small text generation model for demonstration purposes.
|
| 12 |
+
# For larger models, ensure your Hugging Face Space has sufficient GPU resources.
|
| 13 |
+
print("\nLoading text generation pipeline...")
|
| 14 |
+
# You might need to install 'accelerate' and 'bitsandbytes' for quantized models or faster inference
|
| 15 |
+
# !pip install -q accelerate bitsandbytes
|
| 16 |
+
|
| 17 |
+
# This model is small and runs well on CPU for basic demonstrations.
|
| 18 |
+
# For a more capable LLM, you'd choose models like 'google/gemma-2b-it' or 'TinyLlama/TinyLlama-1.1B-Chat-v1.0'
|
| 19 |
+
# but these would require a GPU runtime on your Space.
|
| 20 |
+
|
| 21 |
+
# For this example, let's pick a very small model for fast execution even on CPU.
|
| 22 |
+
# For a true LLM, consider a model like 'distilbert-base-uncased' for text classification or 'gpt2' for generation,
|
| 23 |
+
# though 'gpt2' can be slow without GPU.
|
| 24 |
+
|
| 25 |
+
# Let's use a simple sentiment analysis model as it's quick and illustrative.
|
| 26 |
+
# If a full text generation LLM is desired, 'gpt2' is a good choice if a GPU is available or patience is high.
|
| 27 |
+
# For a quick CPU example of a small text model, let's use a fill-mask pipeline.
|
| 28 |
+
|
| 29 |
+
# Fallback for LLM-like behavior with quick execution (e.g., fill-mask)
|
| 30 |
+
# A true 'text-generation' LLM would be 'gpt2' but it's slower.
|
| 31 |
+
# Using a small 'fill-mask' model to simulate interactive LLM-like behavior quickly.
|
| 32 |
+
# If you have GPU, you can switch to: pipeline("text-generation", model="gpt2")
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Attempt to load a text-generation model first
|
| 36 |
+
generator = pipeline("text-generation", model="distilgpt2", device=0) # device=0 for GPU if available
|
| 37 |
+
print("Using distilgpt2 for text generation.")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Could not load distilgpt2 for text-generation, falling back to fill-mask: {e}")
|
| 40 |
+
generator = pipeline("fill-mask", model="distilbert-base-uncased")
|
| 41 |
+
print("Using distilbert-base-uncased for fill-mask.")
|
| 42 |
+
|
| 43 |
+
print("Model loaded.")
|
| 44 |
+
|
| 45 |
+
# --- Step 2: Define the LLM inference function --- #
|
| 46 |
+
def llm_inference(prompt):
|
| 47 |
+
if generator.task == "text-generation":
|
| 48 |
+
response = generator(prompt, max_new_tokens=50, num_return_sequences=1)
|
| 49 |
+
return response[0]['generated_text']
|
| 50 |
+
elif generator.task == "fill-mask":
|
| 51 |
+
response = generator(prompt)
|
| 52 |
+
# For fill-mask, return the top predicted token and its score
|
| 53 |
+
top_prediction = response[0]
|
| 54 |
+
return f"{prompt.replace('[MASK]', f'**{top_prediction['token_str']}**')} (Score: {top_prediction['score']:.2f})"
|
| 55 |
+
|
| 56 |
+
# --- Step 3: Create a Gradio Interface --- #
|
| 57 |
+
print("\nBuilding Gradio interface...")
|
| 58 |
+
if generator.task == "text-generation":
|
| 59 |
+
interface = gr.Interface(
|
| 60 |
+
fn=llm_inference,
|
| 61 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your prompt here..."),
|
| 62 |
+
outputs=gr.Textbox(lines=5),
|
| 63 |
+
title="Simple LLM Chatbot (distilgpt2)",
|
| 64 |
+
description="Enter a prompt and get text generated by a small LLM."
|
| 65 |
+
)
|
| 66 |
+
else:
|
| 67 |
+
interface = gr.Interface(
|
| 68 |
+
fn=llm_inference,
|
| 69 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter a sentence with [MASK] to fill, e.g., 'The capital of France is [MASK].'"),
|
| 70 |
+
outputs=gr.Textbox(lines=5),
|
| 71 |
+
title="Masked Language Model (distilbert)",
|
| 72 |
+
description="Enter a sentence with a [MASK] token to see the model's prediction."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|