Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file specifies intentionally untracked files that Git should ignore.
# It helps keep the repository clean by excluding temporary files, build artifacts,
# and environment-specific configurations.

__pycache__/
*.pyc
*.log
.venv/
35 changes: 35 additions & 0 deletions assignment_1/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Import necessary libraries
from fastapi import FastAPI
from transformers import pipeline
from pydantic import BaseModel

# Create a FastAPI application instance
app = FastAPI()

# Load the text generation pipeline from Hugging Face with the distilgpt2 model
generator = pipeline('text-generation', model='distilgpt2')

# Define a Pydantic model for the request body
class Prompt(BaseModel):
text: str

# Define the root endpoint of the API
@app.get("/")
def read_root():
"""
A simple endpoint that returns a welcome message.
"""
return {"message": "Welcome to the Generative AI Exercises!"}

# Define an endpoint for text generation
@app.post("/hello-llm")
def generate_text(prompt: Prompt):
"""
Generates text using the distilgpt2 model based on the provided prompt.
"""
# Use the generator to create text, limiting the length to 50 tokens
result = generator(prompt.text, max_length=50, num_return_sequences=1)
# Return the generated text
return {"generated_text": result[0]['generated_text']}

# RUN: curl -X POST -H "Content-Type: application/json" -d "{\"text\": \"Hello, this is a test.\"}"http://localhost:8000/hello-llm
53 changes: 53 additions & 0 deletions assignment_10/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Import necessary libraries
from fastapi import FastAPI
from pydantic import BaseModel
from diffusers import StableDiffusionPipeline
import torch
from io import BytesIO
from starlette.responses import StreamingResponse

# Create a FastAPI application instance
app = FastAPI()

# Load the Stable Diffusion pipeline
# This will download the model, which is several gigabytes in size.
# It is highly recommended to run this on a machine with a GPU for performance.
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
)
# If a CUDA-enabled GPU is available, move the pipeline to the GPU for faster processing.
if torch.cuda.is_available():
pipe = pipe.to("cuda")

# Define a Pydantic model for the text prompt in the request body
class Prompt(BaseModel):
text: str

# Define an endpoint for generating images
@app.post("/generate-image")
def generate_image(prompt: Prompt):
"""
Generates an image from a text prompt using the Stable Diffusion model.
Note: Image generation on a CPU will be very slow.
"""
# Generate an image based on the provided text prompt.
# The .images[0] extracts the first generated image.
image = pipe(prompt.text).images[0]

# Save the generated image to a byte buffer in PNG format.
buffer = BytesIO()
image.save(buffer, format="PNG")
# Reset the buffer's position to the beginning.
buffer.seek(0)

# Return the image as a streaming response with the appropriate media type.
return StreamingResponse(buffer, media_type="image/png")

# Define the root endpoint of the API
@app.get("/")
def read_root():
"""
A simple endpoint that returns a welcome message for the Image Generation API.
"""
return {"message": "Welcome to the Image Generation API!"}
69 changes: 69 additions & 0 deletions assignment_2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
"""
FastAPI application for text summarization using a Hugging Face BART model.

This application provides a web API endpoint to summarize long texts.
It leverages the 'transformers' library to utilize pre-trained models
for sequence-to-sequence summarization.
"""

# Import necessary libraries for building the API and handling data.
from fastapi import FastAPI # FastAPI framework for building web APIs.
from transformers import pipeline # Hugging Face's pipeline for easy use of pre-trained models.
from pydantic import BaseModel # Used for data validation and settings management.

# Create a FastAPI application instance.
# This is the main entry point for the web application.
app = FastAPI()

# Load the summarization pipeline from Hugging Face.
# The 'facebook/bart-large-cnn' model is a pre-trained sequence-to-sequence model
# optimized for summarization tasks. This step downloads and loads the model
# into memory, which can take some time and requires an internet connection.
# Changes to the model name here would load a different summarization model.
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

# Define a Pydantic model for the request body of the /summarize endpoint.
# This class specifies the expected structure and data types for incoming JSON payloads.
# If the incoming request body does not match this schema, FastAPI will automatically
# return a validation error.
class TextToSummarize(BaseModel):
text: str # The input text string that needs to be summarized.

# Define the root endpoint of the API.
# This is a GET request handler for the base URL ("/").
@app.get("/")
def read_root():
"""
A simple endpoint that returns a welcome message for the API.
This can be used to check if the API is running.
"""
return {"message": "Welcome to the Generative AI Exercises!"}

# Define an endpoint for text summarization.
# This is a POST request handler for the "/summarize" URL.
# It expects a JSON payload conforming to the TextToSummarize Pydantic model.
@app.post("/summarize")
def summarize_text(data: TextToSummarize):
"""
Summarizes a long text using the pre-trained 'facebook/bart-large-cnn' model.

Args:
data (TextToSummarize): The request body containing the text to be summarized.

Returns:
dict: A dictionary containing the generated summary under the key 'summary'.

Potential Impact of Changes:
- Changing 'max_length' or 'min_length' will alter the length of the generated summary.
- Setting 'do_sample=True' would introduce more randomness into the summary generation.
- If the 'summarizer' model is changed (e.g., to a different language model),
the quality and style of the summaries will change accordingly.
"""
# Use the loaded summarizer pipeline to generate a summary.
# 'data.text' provides the input text from the request body.
# 'max_length' and 'min_length' control the output summary length.
# 'do_sample=False' ensures deterministic (non-random) summary generation.
summary = summarizer(data.text, max_length=130, min_length=30, do_sample=False)
# The pipeline returns a list of dictionaries; we extract the 'summary_text' from the first result.
# Return the generated summary in a JSON-compatible dictionary.
return {"summary": summary[0]['summary_text']}
33 changes: 33 additions & 0 deletions assignment_3/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Import necessary libraries
from fastapi import FastAPI
from transformers import pipeline
from pydantic import BaseModel

# Create a FastAPI application instance
app = FastAPI()

# Load the sentiment analysis pipeline from Hugging Face using an explicit model name
sentiment_analyzer = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")

# Define a Pydantic model for the request body
class TextToAnalyze(BaseModel):
text: str

# Define the root endpoint of the API
@app.get("/")
def read_root():
"""
A simple endpoint that returns a welcome message.
"""
return {"message": "Welcome to the Sentiment Analysis API!"}

# Define an endpoint for sentiment analysis
@app.post("/sentiment")
def analyze_sentiment(data: TextToAnalyze):
"""
Analyzes the sentiment of a text using the distilbert-base-uncased-finetuned-sst-2-english model.
"""
# Use the sentiment_analyzer to analyze the sentiment of the text
result = sentiment_analyzer(data.text)
# Return the sentiment analysis result
return {"sentiment": result[0]}
35 changes: 35 additions & 0 deletions assignment_4/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Import necessary libraries
from fastapi import FastAPI, File, UploadFile
from transformers import pipeline
from PIL import Image
import io

# Create a FastAPI application instance
app = FastAPI()

# Load the image captioning pipeline from Hugging Face with the vit-gpt2-image-captioning model
captioner = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")

# Define the root endpoint of the API
@app.get("/")
def read_root():
"""
A simple endpoint that returns a welcome message.
"""
return {"message": "Welcome to the Image Captioning API!"}

# Define an endpoint for image captioning
@app.post("/caption-image")
async def caption_image(file: UploadFile = File(...)):
"""
Generates a caption for an uploaded image using the vit-gpt2-image-captioning model.
"""
# Read the contents of the uploaded image file
contents = await file.read()
# Open the image using PIL (Python Imaging Library)
image = Image.open(io.BytesIO(contents))

# Generate a caption for the image
result = captioner(image)
# Return the generated caption
return {"caption": result[0]['generated_text']}
56 changes: 56 additions & 0 deletions assignment_5/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Import necessary libraries
from fastapi import FastAPI
from pydantic import BaseModel
from langchain.vectorstores import Chroma
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Create a FastAPI application instance
app = FastAPI()

# 1. Load and process the documents
# Define a list of documents that will form the knowledge base
documents = [
"The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France.",
"It is named after the engineer Gustave Eiffel, whose company designed and built the tower.",
"Constructed from 1887 to 1889 as the entrance to the 1889 World's Fair, it was initially criticized by some of France's leading artists and intellectuals for its design, but it has become a global cultural icon of France and one of the most recognizable structures in the world.",
"The tower is 330 metres (1,083 ft) tall, about the same height as an 81-storey building, and is the tallest structure in Paris.",
"Its base is square, measuring 125 metres (410 ft) on each side."
]
# Initialize a text splitter to break down long documents into smaller chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
# Create document objects from the raw text
texts = text_splitter.create_documents(documents)

# 2. Create embeddings
# Initialize Hugging Face embeddings to convert text into numerical vectors
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")

# 3. Create a Chroma vector store and retriever
# Create a Chroma vector store from the documents and their embeddings
vectorstore = Chroma.from_documents(texts, embeddings)
# Create a retriever to search for relevant documents in the vector store
retriever = vectorstore.as_retriever()

# Define a Pydantic model for the query request body
class Query(BaseModel):
question: str

# Define an endpoint for querying the RAG system
@app.post("/rag-query")
def rag_query(query: Query):
"""
Queries the RAG system with a given question and returns the most relevant documents.
"""
# Retrieve relevant documents based on the user's question
docs = retriever.get_relevant_documents(query.question)
# Return the content of the relevant documents
return {"relevant_documents": [doc.page_content for doc in docs]}

# Define the root endpoint of the API
@app.get("/")
def read_root():
"""
A simple endpoint that returns a welcome message.
"""
return {"message": "Welcome to the RAG with Chroma and LangChain API!"}
56 changes: 56 additions & 0 deletions assignment_6/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Import necessary libraries
from fastapi import FastAPI
from pydantic import BaseModel
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter

# Create a FastAPI application instance
app = FastAPI()

# 1. Load and process the documents
# Define a list of documents that will form the knowledge base
documents = [
"The Eiffel Tower is a wrought-iron lattice tower on the Champ de Mars in Paris, France.",
"It is named after the engineer Gustave Eiffel, whose company designed and built the tower.",
"Constructed from 1887 to 1889 as the entrance to the 1889 World's Fair, it was initially criticized by some of France's leading artists and intellectuals for its design, but it has become a global cultural icon of France and one of the most recognizable structures in the world.",
"The tower is 330 metres (1,083 ft) tall, about the same height as an 81-storey building, and is the tallest structure in Paris.",
"Its base is square, measuring 125 metres (410 ft) on each side."
]
# Initialize a text splitter to break down long documents into smaller chunks
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
# Create document objects from the raw text
texts = text_splitter.create_documents(documents)

# 2. Create embeddings
# Initialize Hugging Face embeddings to convert text into numerical vectors
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")

# 3. Create a FAISS vector store and retriever
# Create a FAISS vector store from the documents and their embeddings
vectorstore = FAISS.from_documents(texts, embeddings)
# Create a retriever to search for relevant documents in the vector store
retriever = vectorstore.as_retriever()

# Define a Pydantic model for the query request body
class Query(BaseModel):
question: str

# Define an endpoint for querying the RAG system
@app.post("/rag-faiss-query")
def rag_faiss_query(query: Query):
"""
Queries the RAG system with a given question and returns the most relevant documents, using FAISS.
"""
# Retrieve relevant documents based on the user's question
docs = retriever.get_relevant_documents(query.question)
# Return the content of the relevant documents
return {"relevant_documents": [doc.page_content for doc in docs]}

# Define the root endpoint of the API
@app.get("/")
def read_root():
"""
A simple endpoint that returns a welcome message.
"""
return {"message": "Welcome to the RAG with FAISS and LangChain API!"}
35 changes: 35 additions & 0 deletions assignment_7/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Import necessary libraries
from fastapi import FastAPI, File, UploadFile, Form
from transformers import pipeline
from PIL import Image
import io

# Create a FastAPI application instance
app = FastAPI()

# Load the Visual Question Answering pipeline from Hugging Face with the Salesforce/blip-vqa-base model
vqa_pipeline = pipeline("visual-question-answering", model="Salesforce/blip-vqa-base")

# Define the root endpoint of the API
@app.get("/")
def read_root():
"""
A simple endpoint that returns a welcome message.
"""
return {"message": "Welcome to the Visual Question Answering API!"}

# Define an endpoint for Visual Question Answering
@app.post("/qa-image-text")
async def qa_image_text(file: UploadFile = File(...), question: str = Form(...)):
"""
Answers a question about an uploaded image using the Salesforce/blip-vqa-base model.
"""
# Read the contents of the uploaded image file
contents = await file.read()
# Open the image using PIL (Python Imaging Library)
image = Image.open(io.BytesIO(contents))

# Use the VQA pipeline to answer the question about the image
result = vqa_pipeline(image=image, question=question)
# Return the answer
return {"answer": result[0]['answer']}
Loading