Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- README.md +43 -13
- ai_wrapper.py +88 -0
- app.py +176 -0
- app_hf.py +15 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,13 +1,43 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# DDS AI Project Assistant
|
| 2 |
+
|
| 3 |
+
A Generative AI project assistant powered by OpenAI GPT.
|
| 4 |
+
|
| 5 |
+
## Setup Instructions
|
| 6 |
+
|
| 7 |
+
### Local Development
|
| 8 |
+
1. Clone the repository
|
| 9 |
+
2. Create a `.env` file with your OpenAI API key:
|
| 10 |
+
```
|
| 11 |
+
OPENAI_API_KEY=your-api-key-here
|
| 12 |
+
```
|
| 13 |
+
3. Install dependencies:
|
| 14 |
+
```
|
| 15 |
+
pip install -r requirements.txt
|
| 16 |
+
```
|
| 17 |
+
4. Run the app:
|
| 18 |
+
```
|
| 19 |
+
python app.py
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
### Hugging Face Spaces Deployment
|
| 23 |
+
1. Create a new Space on Hugging Face
|
| 24 |
+
2. Choose "Gradio" as the SDK
|
| 25 |
+
3. Add your OpenAI API key as a secret:
|
| 26 |
+
- Go to Settings > Repository Secrets
|
| 27 |
+
- Add `OPENAI_API_KEY` with your API key
|
| 28 |
+
4. Upload the following files:
|
| 29 |
+
- app_hf.py (rename to app.py in Spaces)
|
| 30 |
+
- ai_wrapper.py
|
| 31 |
+
- requirements.txt
|
| 32 |
+
|
| 33 |
+
## Features
|
| 34 |
+
- Interactive AI project brainstorming
|
| 35 |
+
- Code generation assistance
|
| 36 |
+
- 10 popular GenAI project templates
|
| 37 |
+
- Beautiful Gradio interface
|
| 38 |
+
|
| 39 |
+
## Environment Variables
|
| 40 |
+
- `OPENAI_API_KEY`: Your OpenAI API key (required)
|
| 41 |
+
|
| 42 |
+
## License
|
| 43 |
+
MIT License
|
ai_wrapper.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
from typing import Dict, Any, Optional
|
| 3 |
+
|
| 4 |
+
class AIProjectAssistant:
|
| 5 |
+
"""Wrapper class for OpenAI API interactions focused on Data Science and AI projects."""
|
| 6 |
+
|
| 7 |
+
def __init__(self, api_key: str):
|
| 8 |
+
"""Initialize the AI assistant with OpenAI API key."""
|
| 9 |
+
openai.api_key = api_key
|
| 10 |
+
self.default_model = "gpt-4"
|
| 11 |
+
self.default_temperature = 0.7
|
| 12 |
+
|
| 13 |
+
def send_prompt(self,
|
| 14 |
+
prompt: str,
|
| 15 |
+
model: Optional[str] = None,
|
| 16 |
+
temperature: Optional[float] = None) -> Dict[str, Any]:
|
| 17 |
+
"""
|
| 18 |
+
Send a prompt to OpenAI API and get the response.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
prompt (str): The user's input prompt
|
| 22 |
+
model (str, optional): OpenAI model to use. Defaults to gpt-4
|
| 23 |
+
temperature (float, optional): Response creativity (0-1). Defaults to 0.7
|
| 24 |
+
|
| 25 |
+
Returns:
|
| 26 |
+
Dict[str, Any]: OpenAI API response
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
response = openai.ChatCompletion.create(
|
| 30 |
+
model=model or self.default_model,
|
| 31 |
+
messages=[
|
| 32 |
+
{"role": "user", "content": prompt}
|
| 33 |
+
],
|
| 34 |
+
temperature=temperature or self.default_temperature
|
| 35 |
+
)
|
| 36 |
+
return {
|
| 37 |
+
"success": True,
|
| 38 |
+
"content": response.choices[0].message.content,
|
| 39 |
+
"tokens_used": response.usage.total_tokens
|
| 40 |
+
}
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return {
|
| 43 |
+
"success": False,
|
| 44 |
+
"error": str(e)
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
def brainstorm_project(self, topic: str) -> Dict[str, Any]:
|
| 48 |
+
"""
|
| 49 |
+
Generate AI project ideas based on a topic.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
topic (str): The topic or field of interest
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
Dict[str, Any]: Project suggestions and implementation details
|
| 56 |
+
"""
|
| 57 |
+
prompt = f"""
|
| 58 |
+
Help me brainstorm an AI/Data Science project related to {topic}. Please provide:
|
| 59 |
+
1. Project title
|
| 60 |
+
2. Problem statement
|
| 61 |
+
3. Suggested approach
|
| 62 |
+
4. Required technologies/libraries
|
| 63 |
+
5. Potential challenges
|
| 64 |
+
6. Expected outcomes
|
| 65 |
+
"""
|
| 66 |
+
return self.send_prompt(prompt)
|
| 67 |
+
|
| 68 |
+
def get_code_suggestion(self, description: str) -> Dict[str, Any]:
|
| 69 |
+
"""
|
| 70 |
+
Get code suggestions for implementing specific functionality.
|
| 71 |
+
|
| 72 |
+
Args:
|
| 73 |
+
description (str): Description of the desired functionality
|
| 74 |
+
|
| 75 |
+
Returns:
|
| 76 |
+
Dict[str, Any]: Code suggestions and explanations
|
| 77 |
+
"""
|
| 78 |
+
prompt = f"""
|
| 79 |
+
Please provide Python code suggestions for the following functionality:
|
| 80 |
+
{description}
|
| 81 |
+
|
| 82 |
+
Include:
|
| 83 |
+
1. Code implementation
|
| 84 |
+
2. Required imports
|
| 85 |
+
3. Brief explanation of the approach
|
| 86 |
+
4. Any potential improvements
|
| 87 |
+
"""
|
| 88 |
+
return self.send_prompt(prompt)
|
app.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ai_wrapper import AIProjectAssistant
|
| 3 |
+
from typing import Tuple
|
| 4 |
+
import os
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
|
| 7 |
+
# Load environment variables
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Get API key from environment variable
|
| 11 |
+
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 12 |
+
if not API_KEY:
|
| 13 |
+
raise ValueError("No OpenAI API key found. Please set OPENAI_API_KEY environment variable.")
|
| 14 |
+
|
| 15 |
+
# Initialize AI Assistant
|
| 16 |
+
assistant = AIProjectAssistant(API_KEY)
|
| 17 |
+
|
| 18 |
+
# Updated project options focusing on Generative AI
|
| 19 |
+
PROJECT_OPTIONS = {
|
| 20 |
+
"Text-to-Image Generation": "Create an AI system that generates images from text descriptions using models like DALL-E or Stable Diffusion",
|
| 21 |
+
"GPT Chatbot Assistant": "Build a custom GPT-powered chatbot assistant for specific domain expertise",
|
| 22 |
+
"AI Story Generator": "Develop a creative writing AI that generates stories based on prompts",
|
| 23 |
+
"Voice Cloning AI": "Create a system that can clone and synthesize human voices",
|
| 24 |
+
"AI Music Composer": "Build an AI system that composes original music in different styles",
|
| 25 |
+
"Text-to-Video Generation": "Implement a system that creates short videos from text descriptions",
|
| 26 |
+
"AI Code Generator": "Create a coding assistant that generates code from natural language descriptions",
|
| 27 |
+
"AI Art Style Transfer": "Develop a system that applies artistic styles to images using AI",
|
| 28 |
+
"AI Content Summarizer": "Build an AI that creates concise summaries of long-form content",
|
| 29 |
+
"Virtual Avatar Creator": "Create an AI system that generates and animates virtual avatars"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
# Custom CSS for better styling
|
| 33 |
+
CUSTOM_CSS = """
|
| 34 |
+
.container {
|
| 35 |
+
max-width: 1200px;
|
| 36 |
+
margin: auto;
|
| 37 |
+
padding: 20px;
|
| 38 |
+
}
|
| 39 |
+
.title {
|
| 40 |
+
text-align: center;
|
| 41 |
+
color: #2a9d8f;
|
| 42 |
+
padding: 20px;
|
| 43 |
+
border-bottom: 3px solid #264653;
|
| 44 |
+
margin-bottom: 30px;
|
| 45 |
+
}
|
| 46 |
+
.subtitle {
|
| 47 |
+
color: #264653;
|
| 48 |
+
text-align: center;
|
| 49 |
+
font-style: italic;
|
| 50 |
+
}
|
| 51 |
+
.project-button {
|
| 52 |
+
margin: 5px;
|
| 53 |
+
border: 2px solid #2a9d8f;
|
| 54 |
+
border-radius: 8px;
|
| 55 |
+
background-color: #f8f9fa;
|
| 56 |
+
}
|
| 57 |
+
.project-button:hover {
|
| 58 |
+
background-color: #2a9d8f;
|
| 59 |
+
color: white;
|
| 60 |
+
}
|
| 61 |
+
.output-box {
|
| 62 |
+
border: 2px solid #264653;
|
| 63 |
+
border-radius: 10px;
|
| 64 |
+
padding: 15px;
|
| 65 |
+
}
|
| 66 |
+
.sidebar {
|
| 67 |
+
background-color: #f8f9fa;
|
| 68 |
+
padding: 20px;
|
| 69 |
+
border-radius: 10px;
|
| 70 |
+
border: 2px solid #264653;
|
| 71 |
+
}
|
| 72 |
+
"""
|
| 73 |
+
|
| 74 |
+
def process_input(input_type: str, query: str) -> Tuple[str, str]:
|
| 75 |
+
"""Process user input and return AI response."""
|
| 76 |
+
if not query.strip():
|
| 77 |
+
return "", "Please enter a query"
|
| 78 |
+
|
| 79 |
+
if input_type == "brainstorm":
|
| 80 |
+
response = assistant.brainstorm_project(query)
|
| 81 |
+
else:
|
| 82 |
+
response = assistant.get_code_suggestion(query)
|
| 83 |
+
|
| 84 |
+
if response["success"]:
|
| 85 |
+
status = f"Success! Tokens used: {response.get('tokens_used', 'N/A')}"
|
| 86 |
+
return response["content"], status
|
| 87 |
+
else:
|
| 88 |
+
return "", f"Error: {response.get('error', 'Unknown error occurred')}"
|
| 89 |
+
|
| 90 |
+
def handle_project_click(project_name: str) -> Tuple[str, str, str]:
|
| 91 |
+
"""Handle when a project option is clicked."""
|
| 92 |
+
description = PROJECT_OPTIONS[project_name]
|
| 93 |
+
response = assistant.brainstorm_project(description)
|
| 94 |
+
|
| 95 |
+
if response["success"]:
|
| 96 |
+
status = f"Success! Tokens used: {response.get('tokens_used', 'N/A')}"
|
| 97 |
+
return "brainstorm", description, response["content"]
|
| 98 |
+
else:
|
| 99 |
+
return "brainstorm", description, f"Error: {response.get('error', 'Unknown error occurred')}"
|
| 100 |
+
|
| 101 |
+
# Create Gradio interface with custom theme
|
| 102 |
+
with gr.Blocks(css=CUSTOM_CSS, title="DDS AI Project Assistant") as interface:
|
| 103 |
+
with gr.Column(elem_classes="container"):
|
| 104 |
+
gr.HTML("""
|
| 105 |
+
<div class="title">
|
| 106 |
+
<h1>π€ DDS AI Project Assistant π</h1>
|
| 107 |
+
<p class="subtitle">Your Generative AI Project Development Companion</p>
|
| 108 |
+
</div>
|
| 109 |
+
""")
|
| 110 |
+
|
| 111 |
+
with gr.Row():
|
| 112 |
+
# Left sidebar with project options
|
| 113 |
+
with gr.Column(scale=1, elem_classes="sidebar"):
|
| 114 |
+
gr.HTML("""
|
| 115 |
+
<h3 style="text-align: center; color: #2a9d8f;">
|
| 116 |
+
π― Popular GenAI Projects
|
| 117 |
+
</h3>
|
| 118 |
+
""")
|
| 119 |
+
project_buttons = [
|
| 120 |
+
gr.Button(
|
| 121 |
+
name,
|
| 122 |
+
elem_classes="project-button"
|
| 123 |
+
) for name in PROJECT_OPTIONS.keys()
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
# Main content area
|
| 127 |
+
with gr.Column(scale=3):
|
| 128 |
+
input_type = gr.Radio(
|
| 129 |
+
choices=["brainstorm", "code"],
|
| 130 |
+
label="π€ What kind of help do you need?",
|
| 131 |
+
value="brainstorm"
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
query = gr.Textbox(
|
| 135 |
+
label="π Enter your topic or code request",
|
| 136 |
+
placeholder="e.g., 'text-to-image generation' or 'implement stable diffusion'",
|
| 137 |
+
elem_classes="output-box"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
submit_btn = gr.Button(
|
| 141 |
+
"π Get AI Assistance",
|
| 142 |
+
elem_classes="project-button"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
with gr.Column(elem_classes="output-box"):
|
| 146 |
+
output = gr.Textbox(
|
| 147 |
+
label="π€ AI Response",
|
| 148 |
+
lines=10
|
| 149 |
+
)
|
| 150 |
+
status = gr.Textbox(
|
| 151 |
+
label="π Status"
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
gr.HTML("""
|
| 155 |
+
<div style="text-align: center; margin-top: 20px; padding: 10px; border-top: 2px solid #264653;">
|
| 156 |
+
<p>Developed by DDS Team | Powered by OpenAI</p>
|
| 157 |
+
</div>
|
| 158 |
+
""")
|
| 159 |
+
|
| 160 |
+
# Handle main submit button
|
| 161 |
+
submit_btn.click(
|
| 162 |
+
fn=process_input,
|
| 163 |
+
inputs=[input_type, query],
|
| 164 |
+
outputs=[output, status]
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# Handle project option buttons
|
| 168 |
+
for btn in project_buttons:
|
| 169 |
+
btn.click(
|
| 170 |
+
fn=handle_project_click,
|
| 171 |
+
inputs=[btn],
|
| 172 |
+
outputs=[input_type, query, output]
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
interface.launch()
|
app_hf.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from ai_wrapper import AIProjectAssistant
|
| 3 |
+
from typing import Tuple
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# For Hugging Face Spaces, get API key from environment variable
|
| 7 |
+
API_KEY = os.environ.get("OPENAI_API_KEY")
|
| 8 |
+
if not API_KEY:
|
| 9 |
+
raise ValueError("No OpenAI API key found. Please set OPENAI_API_KEY in Hugging Face Spaces secrets.")
|
| 10 |
+
|
| 11 |
+
# Initialize AI Assistant
|
| 12 |
+
assistant = AIProjectAssistant(API_KEY)
|
| 13 |
+
|
| 14 |
+
# Rest of your code from app.py...
|
| 15 |
+
# (Copy everything else from app.py, starting from PROJECT_OPTIONS)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
openai==0.28.0
|
| 3 |
+
python-dotenv>=1.0.0
|
| 4 |
+
typing-extensions>=4.0.0
|