Add notebooks for Muhammad Qasim Sheikh in community-contributions

This commit is contained in:
aashahid
2025-10-21 17:51:37 +05:00
parent ef34387aee
commit 0b4e4be9a0
12 changed files with 1284 additions and 0 deletions

View File

@@ -0,0 +1,224 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "4ef1e715",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import gradio as gr\n",
"from openai import OpenAI\n",
"from dotenv import load_dotenv"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3426558",
"metadata": {},
"outputs": [],
"source": [
"# Load API key\n",
"load_dotenv()\n",
"client = OpenAI(api_key=os.getenv(\"OPENAI_API_KEY\"))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e18a59a3",
"metadata": {},
"outputs": [],
"source": [
"# -------------------------------\n",
"# Helper: Prompt Builder\n",
"# -------------------------------\n",
"def build_prompt(task, topic, tone, audience):\n",
" task_prompts = {\n",
" \"Brochure\": f\"Write a compelling marketing brochure about {topic}.\",\n",
" \"Blog Post\": f\"Write a blog post on {topic} with engaging storytelling and useful insights.\",\n",
" \"Product Comparison\": f\"Write a product comparison summary focusing on {topic}, including pros, cons, and recommendations.\",\n",
" \"Idea Brainstorm\": f\"Brainstorm creative ideas or solutions related to {topic}.\"\n",
" }\n",
" base = task_prompts.get(task, \"Write something creative.\")\n",
" if tone:\n",
" base += f\" Use a {tone} tone.\"\n",
" if audience:\n",
" base += f\" Tailor it for {audience}.\"\n",
" return base"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "65a27bfb",
"metadata": {},
"outputs": [],
"source": [
"# -------------------------------\n",
"# Generate with multiple models\n",
"# -------------------------------\n",
"def generate_stream(task, topic, tone, audience, model):\n",
" if not topic.strip():\n",
" yield \"⚠️ Please enter a topic.\"\n",
" return\n",
"\n",
" prompt = build_prompt(task, topic, tone, audience)\n",
"\n",
" stream = client.chat.completions.create(\n",
" model=model,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": prompt}\n",
" ],\n",
" max_tokens=800,\n",
" stream=True\n",
" )\n",
"\n",
" result = \"\"\n",
" for chunk in stream:\n",
" result += chunk.choices[0].delta.content or \"\"\n",
" yield result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9e15abee",
"metadata": {},
"outputs": [],
"source": [
"# -------------------------------\n",
"# Refinement logic\n",
"# -------------------------------\n",
"def refine_stream(original_text, instruction, model):\n",
" if not original_text.strip():\n",
" yield \"⚠️ Please paste the text you want to refine.\"\n",
" return\n",
" if not instruction.strip():\n",
" yield \"⚠️ Please provide a refinement instruction.\"\n",
" return\n",
"\n",
" refined_prompt = f\"Refine the following text based on this instruction: {instruction}\\n\\nText:\\n{original_text}\"\n",
"\n",
" stream = client.chat.completions.create(\n",
" model=model,\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a writing assistant.\"},\n",
" {\"role\": \"user\", \"content\": refined_prompt}\n",
" ],\n",
" max_tokens=800,\n",
" stream=True\n",
" )\n",
"\n",
" result = \"\"\n",
" for chunk in stream:\n",
" result += chunk.choices[0].delta.content or \"\"\n",
" yield result\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8ee02feb",
"metadata": {},
"outputs": [],
"source": [
"# -------------------------------\n",
"# Gradio UI\n",
"# -------------------------------\n",
"with gr.Blocks(title=\"AI Creative Studio\") as demo:\n",
" gr.Markdown(\"# AI Creative Studio\\nGenerate marketing content, blog posts, or creative ideas — streamed in real-time!\")\n",
"\n",
" with gr.Row():\n",
" task = gr.Dropdown(\n",
" [\"Brochure\", \"Blog Post\", \"Product Comparison\", \"Idea Brainstorm\"],\n",
" label=\"Task Type\",\n",
" value=\"Brochure\"\n",
" )\n",
" topic = gr.Textbox(label=\"Topic\", placeholder=\"e.g., Electric Cars, AI in Education...\")\n",
" with gr.Row():\n",
" tone = gr.Textbox(label=\"Tone (optional)\", placeholder=\"e.g., professional, casual, humorous...\")\n",
" audience = gr.Textbox(label=\"Target Audience (optional)\", placeholder=\"e.g., investors, students, developers...\")\n",
"\n",
" model = gr.Dropdown(\n",
" [\"gpt-4o-mini\", \"gpt-3.5-turbo\", \"gpt-4\"],\n",
" label=\"Choose a model\",\n",
" value=\"gpt-4o-mini\"\n",
" )\n",
"\n",
" generate_btn = gr.Button(\"Generate Content\")\n",
" output_md = gr.Markdown(label=\"Generated Content\", show_label=True)\n",
"\n",
" generate_btn.click(\n",
" fn=generate_stream,\n",
" inputs=[task, topic, tone, audience, model],\n",
" outputs=output_md\n",
" )\n",
"\n",
" gr.Markdown(\"---\\n## Refine Your Content\")\n",
"\n",
" original_text = gr.Textbox(\n",
" label=\"Original Content\",\n",
" placeholder=\"Paste content you want to refine...\",\n",
" lines=10\n",
" )\n",
" instruction = gr.Textbox(\n",
" label=\"Refinement Instruction\",\n",
" placeholder=\"e.g., Make it shorter and more persuasive.\",\n",
" )\n",
" refine_model = gr.Dropdown(\n",
" [\"gpt-4o-mini\", \"gpt-3.5-turbo\", \"gpt-4\"],\n",
" label=\"Model for Refinement\",\n",
" value=\"gpt-4o-mini\"\n",
" )\n",
"\n",
" refine_btn = gr.Button(\"Refine\")\n",
" refined_output = gr.Markdown(label=\"Refined Content\", show_label=True)\n",
"\n",
" refine_btn.click(\n",
" fn=refine_stream,\n",
" inputs=[original_text, instruction, refine_model],\n",
" outputs=refined_output\n",
" )\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55d42c7e",
"metadata": {},
"outputs": [],
"source": [
"# -------------------------------\n",
"# Launch the App\n",
"# -------------------------------\n",
"if __name__ == \"__main__\":\n",
" demo.launch()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "llm-engineering",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@@ -0,0 +1,48 @@
# AI Creative Studio
## Project Overview
AI Creative Studio is a web-based application built with Gradio that allows users to generate and refine high-quality written content in real time using OpenAI language models. It is designed as a flexible creative tool for content creation tasks such as writing brochures, blog posts, product comparisons, and brainstorming ideas. The application also supports interactive refinement, enabling users to improve or adapt existing text based on specific instructions.
The core idea is to combine the power of OpenAI models with an intuitive, user-friendly interface that streams responses as they are generated. This provides a fast, engaging, and highly interactive writing experience without waiting for the entire response to complete before it appears.
---
## Whats Happening in the Project
1. **Environment Setup and Model Initialization**
- The application loads the OpenAI API key from a `.env` file and initializes the OpenAI client for model interactions.
- Supported models include `gpt-4o-mini`, `gpt-3.5-turbo`, and `gpt-4`, which the user can select from a dropdown menu.
2. **Prompt Construction and Content Generation**
- The `build_prompt` function constructs a task-specific prompt based on the users choices: content type (brochure, blog post, etc.), topic, tone, and target audience.
- Once the user provides the inputs and selects a model, the application sends the prompt to the model.
- The models response is streamed back incrementally, showing text chunk by chunk for a real-time generation experience.
3. **Content Refinement Feature**
- Users can paste existing text and provide a refinement instruction (e.g., “make it more persuasive” or “summarize it”).
- The application then streams an improved version of the text, following the instruction, allowing users to iterate and polish content efficiently.
4. **Gradio User Interface**
- The app is built using Gradio Blocks, providing an organized and interactive layout.
- Key UI elements include:
- Task selection dropdown for choosing the type of content.
- Text inputs for topic, tone, and target audience.
- Model selection dropdown for choosing a specific OpenAI model.
- Real-time markdown display of generated content.
- A refinement panel for improving existing text.
5. **Streaming Workflow**
- Both generation and refinement use OpenAIs streaming API to display the models response as its produced.
- This provides an immediate and responsive user experience, allowing users to see results build up in real time rather than waiting for the entire completion.
---
### Key Features
- Real-time streaming responses for fast and interactive content creation.
- Multiple content generation modes: brochure, blog post, product comparison, and idea brainstorming.
- Customization options for tone and audience to tailor the writing style.
- Interactive refinement tool to enhance or transform existing text.
- Clean and intuitive web interface powered by Gradio.
AI Creative Studio demonstrates how large language models can be integrated into user-facing applications to support creative workflows and improve productivity in content generation and editing.