Practical 10
Working with Huggingface and Gemini LLMs 
Using Huggingface for -
Text generation
Text summarization
Sentiment analysis
Using Gemini for - 
Text generation
Text summarization
Text translation
Hugging face for Text Generation
from transformers import pipeline
generator = pipeline("text-generation",model="gpt2")
prompt="defination of physics"
outputs=generator(prompt,max_new_tokens=50,pad_token_id=50256)
print(outputs[0]["generated_text"])
Output:-
Hugging face for Text summarization
Pip install “transformers<5” torch
from transformers import pipeline
# Load summarization pipeline
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
# Example long texts
articles = [
    "Natural Language Processing (NLP) is a field of artificial intelligence that focuses on the interactions "
    "between computers and human language. It enables tasks such as text classification, translation, "
    "summarization, and question answering. Modern NLP often uses transformer-based models.",
    "Mindfulness is a practice of being aware of the present moment. Research shows it helps reduce stress, "
    "improves concentration, and supports emotional regulation. Many schools and workplaces now incorporate "
    "mindfulness exercises into daily routines."
]
# Summarize both
summaries = summarizer(articles, max_new_tokens=50, min_length=15, do_sample=False)
# Print results
for arti, summ in zip(articles, summaries):
    print("Original:", arti, "\nSummary:", summ['summary_text'], "\n")
Output:-
Huggingface for Sentiment Analysis
from transformers import pipeline
sentiment = pipeline("sentiment-analysis")
texts = [
    "I love teaching Natural Language Processing!",
    "This assignment is terribly confusing.",
    "The workshop was very helpful and fun!",
    "I didn't like the food",
    "I loved the pizza"
]
for t in texts:
    result = sentiment(t)[0]
    print(f"Text: {t}\n - Label: {result['label']}, Score: {result['score']:.4f}\n")
Output:-
Gemini for Text Generation
NOTE  - 
pip install google-generativeai
Generate Gemini API Key by visiting www.aistudio.google.com
Click on Get API Key on the bottom left
Click on Create API Key, copy the key in your program.
Gemini for Text Generation
from google import genai
# Initialize the client
client = genai.Client(api_key="AIzaSyAyFppQ5-u2Twa_JKliZ7JJHziVs_Xaz7g")
# Generate content
response = client.models.generate_content(
    model="gemini-3.6-flash",
    contents="Write a short poem about sunrise."
)
print("Gemini Response:")
print(response.text)
Output:-
Gemini for Text Summarization
from google import genai
# 1. Initialize the client (automatically reads GEMINI_API_KEY from environment variables)
# If you pass api_key explicitly: client = genai.Client(api_key="YOUR_API_KEY")
client = genai.Client(api_key="AIzaSyAyFppQ5-u2Twa_JKliZ7JJHziVs_Xaz7g")
# 2. Define the source text
article = """
Artificial Intelligence (AI) is rapidly transforming education worldwide. 
From personalized learning platforms to automated grading systems, 
AI helps educators save time and provide tailored support to students. 
However, it also raises concerns about bias, privacy, and over-reliance on machines. 
The future of AI in education depends on balancing innovation with ethics and ensuring 
that technology complements, rather than replaces, human teachers.
"""
# 3. Set your prompt
prompt = f"Summarize the following text in one line:\n\n{article}"
# 4. Generate content
response = client.models.generate_content(
    model="gemini-3.6-flash",
    contents=prompt,
)
# 5. Print the response
print("Summary:\n")
print(response.text)
Output:-
Gemini for Language Translation
from google import genai
# Set & initialize the client with the API key
API_KEY = "AIzaSyAyFppQ5-u2Twa_JKliZ7JJHziVs_Xaz7g"
client = genai.Client(api_key=API_KEY)
languages = ["Hindi", "Marathi", "Gujarati", "Tamil", "Telugu"]
# Text for translation
text = "Artificial Intelligence is transforming education and daily life."
# --- PART 1: Loop for multiple languages ---
print("--- Multiple Languages ---")
for lang in languages:
    prompt = f"""Translate the following text into {lang}.
If the language has multiple scripts, use its most commonly used script in India.
Return only the translation, no extra commentary.\n\n{text}"""
    response = client.models.generate_content(
        model="gemini-3.6-flash",
        contents=prompt
    )
    print(f"{lang}: {response.text.strip()}\n")
# --- PART 2: Single Hindi Translation ---
print("--- Single Hindi Translation ---")
hindi_prompt = f"""Translate this text into Hindi.
Return only the translation, no extra commentary.\n\n{text}"""
response_hindi = client.models.generate_content(
    model="gemini-3.6-flash",
    contents=hindi_prompt
)
print("Hindi Translation:")
print(response_hindi.text.strip())
Output:-