Spaces:
Sleeping
Sleeping
Upload 23 files
Browse files- DEPLOYMENT.md +13 -7
- README.md +31 -5
- app.py +112 -25
DEPLOYMENT.md
CHANGED
|
@@ -36,15 +36,15 @@ Upload these files to your Space:
|
|
| 36 |
- `USAGE_GUIDE.md` - User guide
|
| 37 |
- `test_app.py` - Testing script
|
| 38 |
|
| 39 |
-
#### 3. Configure Environment Variables
|
| 40 |
|
| 41 |
-
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
|
| 49 |
**For OpenAI:**
|
| 50 |
```
|
|
@@ -58,6 +58,12 @@ LLM_PROVIDER=anthropic
|
|
| 58 |
ANTHROPIC_API_KEY=your-key-here
|
| 59 |
```
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
#### 4. Space Will Auto-Deploy
|
| 62 |
|
| 63 |
- HuggingFace will automatically build and deploy
|
|
|
|
| 36 |
- `USAGE_GUIDE.md` - User guide
|
| 37 |
- `test_app.py` - Testing script
|
| 38 |
|
| 39 |
+
#### 3. Configure Environment Variables (Optional)
|
| 40 |
|
| 41 |
+
**Default Configuration (Recommended for Quick Start):**
|
| 42 |
|
| 43 |
+
No configuration needed! The app automatically uses HuggingFace Inference API with the built-in `HF_TOKEN`.
|
| 44 |
+
|
| 45 |
+
**Optional: Use Premium Providers**
|
| 46 |
+
|
| 47 |
+
For better performance, you can add these environment variables in Space Settings:
|
| 48 |
|
| 49 |
**For OpenAI:**
|
| 50 |
```
|
|
|
|
| 58 |
ANTHROPIC_API_KEY=your-key-here
|
| 59 |
```
|
| 60 |
|
| 61 |
+
**For Custom HuggingFace Model:**
|
| 62 |
+
```
|
| 63 |
+
LLM_MODEL=mistralai/Mistral-7B-Instruct-v0.2
|
| 64 |
+
# LLM_PROVIDER defaults to huggingface
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
#### 4. Space Will Auto-Deploy
|
| 68 |
|
| 69 |
- HuggingFace will automatically build and deploy
|
README.md
CHANGED
|
@@ -37,6 +37,9 @@ Battle the blank page, reach global audiences, and uncover insights with AI assi
|
|
| 37 |
|
| 38 |
## π Quick Start
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
1. **Generate a Survey**: Start with an outline or topic description
|
| 41 |
2. **Translate**: Select target languages to reach global audiences
|
| 42 |
3. **Collect Responses**: Use the generated survey with your participants
|
|
@@ -44,12 +47,35 @@ Battle the blank page, reach global audiences, and uncover insights with AI assi
|
|
| 44 |
|
| 45 |
## π§ Configuration
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
The app automatically detects which provider to use based on available credentials.
|
| 55 |
|
|
|
|
| 37 |
|
| 38 |
## π Quick Start
|
| 39 |
|
| 40 |
+
**On HuggingFace Spaces:** Works immediately with zero configuration! Uses the free HF Inference API.
|
| 41 |
+
|
| 42 |
+
**Workflow:**
|
| 43 |
1. **Generate a Survey**: Start with an outline or topic description
|
| 44 |
2. **Translate**: Select target languages to reach global audiences
|
| 45 |
3. **Collect Responses**: Use the generated survey with your participants
|
|
|
|
| 47 |
|
| 48 |
## π§ Configuration
|
| 49 |
|
| 50 |
+
### HuggingFace Spaces (Default)
|
| 51 |
+
|
| 52 |
+
**No configuration needed!** The app automatically uses HuggingFace's Inference API.
|
| 53 |
+
|
| 54 |
+
- Uses built-in `HF_TOKEN` (automatically available in Spaces)
|
| 55 |
+
- Default model: `mistralai/Mixtral-8x7B-Instruct-v0.1`
|
| 56 |
+
- Free tier available
|
| 57 |
+
|
| 58 |
+
### Optional: Use Other LLM Providers
|
| 59 |
|
| 60 |
+
For better performance, you can configure alternative providers via environment variables:
|
| 61 |
+
|
| 62 |
+
**OpenAI (Recommended for production):**
|
| 63 |
+
```bash
|
| 64 |
+
LLM_PROVIDER=openai
|
| 65 |
+
OPENAI_API_KEY=sk-your-key-here
|
| 66 |
+
```
|
| 67 |
+
|
| 68 |
+
**Anthropic Claude:**
|
| 69 |
+
```bash
|
| 70 |
+
LLM_PROVIDER=anthropic
|
| 71 |
+
ANTHROPIC_API_KEY=your-key-here
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
**Custom HuggingFace Model:**
|
| 75 |
+
```bash
|
| 76 |
+
LLM_PROVIDER=huggingface
|
| 77 |
+
LLM_MODEL=your-preferred-model
|
| 78 |
+
```
|
| 79 |
|
| 80 |
The app automatically detects which provider to use based on available credentials.
|
| 81 |
|
app.py
CHANGED
|
@@ -23,29 +23,59 @@ current_responses = []
|
|
| 23 |
def initialize_backend():
|
| 24 |
"""Initialize LLM backend based on environment"""
|
| 25 |
try:
|
| 26 |
-
#
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
return LLMBackend(provider=LLMProvider.OPENAI)
|
| 29 |
-
elif os.getenv("ANTHROPIC_API_KEY"):
|
| 30 |
return LLMBackend(provider=LLMProvider.ANTHROPIC)
|
| 31 |
-
elif os.getenv("HUGGINGFACE_API_KEY") or os.getenv("HF_TOKEN"):
|
| 32 |
-
# Use HF_TOKEN which is automatically set in HF Spaces
|
| 33 |
api_key = os.getenv("HUGGINGFACE_API_KEY") or os.getenv("HF_TOKEN")
|
| 34 |
return LLMBackend(provider=LLMProvider.HUGGINGFACE, api_key=api_key)
|
| 35 |
-
|
| 36 |
-
# Fallback to LM Studio for local development
|
| 37 |
return LLMBackend(provider=LLMProvider.LM_STUDIO)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
-
print(f"
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
# Initialize components
|
| 45 |
llm_backend = initialize_backend()
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
|
| 51 |
# ===========================
|
|
@@ -56,6 +86,18 @@ def generate_survey_from_outline(outline: str, survey_type: str, num_questions:
|
|
| 56 |
"""Generate survey from user outline"""
|
| 57 |
global current_survey
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if not outline or not outline.strip():
|
| 60 |
return "β Please provide an outline or topic description.", "", None
|
| 61 |
|
|
@@ -125,6 +167,14 @@ def translate_current_survey(target_languages: List[str]):
|
|
| 125 |
"""Translate the current survey to selected languages"""
|
| 126 |
global current_survey
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
if not current_survey:
|
| 129 |
return "β Please generate or upload a survey first.", "", None
|
| 130 |
|
|
@@ -186,6 +236,14 @@ def get_language_choices():
|
|
| 186 |
|
| 187 |
def analyze_survey_data(responses_json: str, questions_json: str = None):
|
| 188 |
"""Analyze survey responses"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
if not responses_json or not responses_json.strip():
|
| 190 |
return "β Please provide survey responses in JSON format.", "", None
|
| 191 |
|
|
@@ -271,6 +329,16 @@ def create_interface():
|
|
| 271 |
Battle the blank page, reach global audiences, and uncover insights with AI assistance.
|
| 272 |
""")
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
with gr.Tabs() as tabs:
|
| 275 |
|
| 276 |
# ========== SURVEY GENERATION TAB ==========
|
|
@@ -443,20 +511,39 @@ def create_interface():
|
|
| 443 |
- Identify patterns and trends
|
| 444 |
- Generate actionable insights
|
| 445 |
|
| 446 |
-
### π§
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
|
| 448 |
**Supported LLM Providers:**
|
| 449 |
-
-
|
| 450 |
-
-
|
| 451 |
-
-
|
| 452 |
-
- LM Studio (local)
|
| 453 |
-
|
| 454 |
-
**Configuration:**
|
| 455 |
-
Set environment variables to configure your LLM provider:
|
| 456 |
-
- `OPENAI_API_KEY` - For OpenAI models
|
| 457 |
-
- `ANTHROPIC_API_KEY` - For Claude models
|
| 458 |
-
- `HUGGINGFACE_API_KEY` or `HF_TOKEN` - For HuggingFace
|
| 459 |
-
- `LM_STUDIO_URL` - For local LM Studio (default: http://192.168.1.245:1234/v1/chat/completions)
|
| 460 |
|
| 461 |
### π Data Privacy
|
| 462 |
|
|
|
|
| 23 |
def initialize_backend():
|
| 24 |
"""Initialize LLM backend based on environment"""
|
| 25 |
try:
|
| 26 |
+
# Check for explicit provider setting
|
| 27 |
+
provider_env = os.getenv("LLM_PROVIDER", "").lower()
|
| 28 |
+
|
| 29 |
+
# Priority 1: Explicitly set provider
|
| 30 |
+
if provider_env == "openai" and os.getenv("OPENAI_API_KEY"):
|
| 31 |
return LLMBackend(provider=LLMProvider.OPENAI)
|
| 32 |
+
elif provider_env == "anthropic" and os.getenv("ANTHROPIC_API_KEY"):
|
| 33 |
return LLMBackend(provider=LLMProvider.ANTHROPIC)
|
| 34 |
+
elif provider_env == "huggingface" and (os.getenv("HUGGINGFACE_API_KEY") or os.getenv("HF_TOKEN")):
|
|
|
|
| 35 |
api_key = os.getenv("HUGGINGFACE_API_KEY") or os.getenv("HF_TOKEN")
|
| 36 |
return LLMBackend(provider=LLMProvider.HUGGINGFACE, api_key=api_key)
|
| 37 |
+
elif provider_env == "lm_studio":
|
|
|
|
| 38 |
return LLMBackend(provider=LLMProvider.LM_STUDIO)
|
| 39 |
+
|
| 40 |
+
# Priority 2: Auto-detect based on available credentials
|
| 41 |
+
# HF_TOKEN is automatically available in HF Spaces, so check it first
|
| 42 |
+
if os.getenv("HF_TOKEN") or os.getenv("HUGGINGFACE_API_KEY"):
|
| 43 |
+
api_key = os.getenv("HUGGINGFACE_API_KEY") or os.getenv("HF_TOKEN")
|
| 44 |
+
print(f"Auto-detected HuggingFace credentials, using HF Inference API")
|
| 45 |
+
return LLMBackend(provider=LLMProvider.HUGGINGFACE, api_key=api_key)
|
| 46 |
+
elif os.getenv("OPENAI_API_KEY"):
|
| 47 |
+
print(f"Auto-detected OpenAI credentials")
|
| 48 |
+
return LLMBackend(provider=LLMProvider.OPENAI)
|
| 49 |
+
elif os.getenv("ANTHROPIC_API_KEY"):
|
| 50 |
+
print(f"Auto-detected Anthropic credentials")
|
| 51 |
+
return LLMBackend(provider=LLMProvider.ANTHROPIC)
|
| 52 |
+
else:
|
| 53 |
+
# No credentials found - return None to show error in UI
|
| 54 |
+
print("WARNING: No LLM provider credentials found!")
|
| 55 |
+
print("Please set one of: OPENAI_API_KEY, ANTHROPIC_API_KEY, HUGGINGFACE_API_KEY, or HF_TOKEN")
|
| 56 |
+
return None
|
| 57 |
+
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f"Error during backend initialization: {e}")
|
| 60 |
+
import traceback
|
| 61 |
+
traceback.print_exc()
|
| 62 |
+
return None
|
| 63 |
|
| 64 |
|
| 65 |
# Initialize components
|
| 66 |
llm_backend = initialize_backend()
|
| 67 |
+
|
| 68 |
+
# Only initialize if backend is available
|
| 69 |
+
if llm_backend:
|
| 70 |
+
survey_gen = SurveyGenerator(llm_backend)
|
| 71 |
+
survey_trans = SurveyTranslator(llm_backend)
|
| 72 |
+
data_analyzer = DataAnalyzer(llm_backend)
|
| 73 |
+
print(f"β ConversAI initialized with {llm_backend.provider.value} provider")
|
| 74 |
+
else:
|
| 75 |
+
survey_gen = None
|
| 76 |
+
survey_trans = None
|
| 77 |
+
data_analyzer = None
|
| 78 |
+
print("β ConversAI initialization incomplete - no LLM credentials found")
|
| 79 |
|
| 80 |
|
| 81 |
# ===========================
|
|
|
|
| 86 |
"""Generate survey from user outline"""
|
| 87 |
global current_survey
|
| 88 |
|
| 89 |
+
# Check if backend is initialized
|
| 90 |
+
if not survey_gen:
|
| 91 |
+
return (
|
| 92 |
+
"β LLM backend not configured. Please set up API credentials:\n"
|
| 93 |
+
"- For HuggingFace Spaces: HF_TOKEN is auto-available\n"
|
| 94 |
+
"- For OpenAI: Set OPENAI_API_KEY\n"
|
| 95 |
+
"- For Anthropic: Set ANTHROPIC_API_KEY\n"
|
| 96 |
+
"- For HuggingFace: Set HUGGINGFACE_API_KEY",
|
| 97 |
+
"",
|
| 98 |
+
None
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
if not outline or not outline.strip():
|
| 102 |
return "β Please provide an outline or topic description.", "", None
|
| 103 |
|
|
|
|
| 167 |
"""Translate the current survey to selected languages"""
|
| 168 |
global current_survey
|
| 169 |
|
| 170 |
+
# Check if backend is initialized
|
| 171 |
+
if not survey_trans:
|
| 172 |
+
return (
|
| 173 |
+
"β LLM backend not configured. Please set up API credentials in Settings.",
|
| 174 |
+
"",
|
| 175 |
+
None
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
if not current_survey:
|
| 179 |
return "β Please generate or upload a survey first.", "", None
|
| 180 |
|
|
|
|
| 236 |
|
| 237 |
def analyze_survey_data(responses_json: str, questions_json: str = None):
|
| 238 |
"""Analyze survey responses"""
|
| 239 |
+
# Check if backend is initialized
|
| 240 |
+
if not data_analyzer:
|
| 241 |
+
return (
|
| 242 |
+
"β LLM backend not configured. Please set up API credentials in Settings.",
|
| 243 |
+
"",
|
| 244 |
+
None
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
if not responses_json or not responses_json.strip():
|
| 248 |
return "β Please provide survey responses in JSON format.", "", None
|
| 249 |
|
|
|
|
| 329 |
Battle the blank page, reach global audiences, and uncover insights with AI assistance.
|
| 330 |
""")
|
| 331 |
|
| 332 |
+
# Show backend status
|
| 333 |
+
if llm_backend:
|
| 334 |
+
status_msg = f"β
**Active LLM Provider:** {llm_backend.provider.value.upper()} | Model: {llm_backend.model}"
|
| 335 |
+
status_color = "green"
|
| 336 |
+
else:
|
| 337 |
+
status_msg = "β οΈ **No LLM Provider Configured** - Please set API credentials (see About tab for instructions)"
|
| 338 |
+
status_color = "orange"
|
| 339 |
+
|
| 340 |
+
gr.Markdown(f'<div style="background-color: rgba(255, 165, 0, 0.1); padding: 10px; border-radius: 5px; margin: 10px 0;">{status_msg}</div>')
|
| 341 |
+
|
| 342 |
with gr.Tabs() as tabs:
|
| 343 |
|
| 344 |
# ========== SURVEY GENERATION TAB ==========
|
|
|
|
| 511 |
- Identify patterns and trends
|
| 512 |
- Generate actionable insights
|
| 513 |
|
| 514 |
+
### π§ Configuration Guide
|
| 515 |
+
|
| 516 |
+
**For HuggingFace Spaces (Recommended):**
|
| 517 |
+
|
| 518 |
+
No configuration needed! The app automatically uses the HF Inference API with the built-in `HF_TOKEN`.
|
| 519 |
+
|
| 520 |
+
**Supported Models:**
|
| 521 |
+
- Default: `mistralai/Mixtral-8x7B-Instruct-v0.1`
|
| 522 |
+
- You can change by setting `LLM_MODEL` environment variable
|
| 523 |
+
|
| 524 |
+
**For Other LLM Providers:**
|
| 525 |
+
|
| 526 |
+
Add these environment variables in your Space Settings:
|
| 527 |
+
|
| 528 |
+
1. **OpenAI** (Best quality, paid):
|
| 529 |
+
- `LLM_PROVIDER=openai`
|
| 530 |
+
- `OPENAI_API_KEY=sk-your-key`
|
| 531 |
+
|
| 532 |
+
2. **Anthropic Claude** (Best reasoning, paid):
|
| 533 |
+
- `LLM_PROVIDER=anthropic`
|
| 534 |
+
- `ANTHROPIC_API_KEY=your-key`
|
| 535 |
+
|
| 536 |
+
3. **Custom HuggingFace Model**:
|
| 537 |
+
- `LLM_PROVIDER=huggingface`
|
| 538 |
+
- `LLM_MODEL=your-model-name`
|
| 539 |
+
|
| 540 |
+
**π‘ Pro Tip:** For production use, we recommend OpenAI or Anthropic for faster, more reliable results.
|
| 541 |
|
| 542 |
**Supported LLM Providers:**
|
| 543 |
+
- HuggingFace Inference API (Free tier available)
|
| 544 |
+
- OpenAI (GPT-4, GPT-4o-mini, GPT-3.5)
|
| 545 |
+
- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus)
|
| 546 |
+
- LM Studio (local development only)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 547 |
|
| 548 |
### π Data Privacy
|
| 549 |
|