kenichimiyata
commited on
Commit
·
c71e7cc
1
Parent(s):
e461182
Convert to Gradio app for HF Spaces
Browse files- README.md +3 -1
- app.py +36 -97
- requirements.txt +1 -59
README.md
CHANGED
|
@@ -3,7 +3,9 @@ title: FastAPI Django Main Live
|
|
| 3 |
emoji: 🚀
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
-
sdk:
|
|
|
|
|
|
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
| 9 |
---
|
|
|
|
| 3 |
emoji: 🚀
|
| 4 |
colorFrom: blue
|
| 5 |
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.0.1
|
| 8 |
+
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: mit
|
| 11 |
---
|
app.py
CHANGED
|
@@ -1,111 +1,50 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
-
import shutil
|
| 5 |
from dotenv import load_dotenv
|
| 6 |
|
| 7 |
# .envファイルから環境変数を読み込み
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
if not debugpy.is_client_connected():
|
| 16 |
-
print("🔧 デバッグサーバーを起動中...")
|
| 17 |
-
debugpy.listen(("0.0.0.0", 5678))
|
| 18 |
-
print("✅ デバッグサーバーがポート5678で待機中")
|
| 19 |
-
print("💡 VS Codeで 'Remote Attach' を使用してアタッチできます")
|
| 20 |
-
else:
|
| 21 |
-
print("🔗 デバッグクライアントが既に接続されています")
|
| 22 |
-
except ImportError:
|
| 23 |
-
print("⚠️ debugpy がインストールされていません。通常のデバッグモードで継続します")
|
| 24 |
-
except Exception as e:
|
| 25 |
-
print(f"⚠️ デバッグサーバー起動エラー: {e}")
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
import requests
|
| 32 |
-
import uvicorn
|
| 33 |
-
from groq import Groq
|
| 34 |
-
|
| 35 |
-
from fastapi import FastAPI, HTTPException, Header
|
| 36 |
-
from pydantic import BaseModel
|
| 37 |
-
from typing import Any, Coroutine, List
|
| 38 |
-
|
| 39 |
-
from starlette.middleware.cors import CORSMiddleware
|
| 40 |
-
from sse_starlette.sse import EventSourceResponse
|
| 41 |
-
|
| 42 |
-
from groq import AsyncGroq, AsyncStream, Groq
|
| 43 |
-
from groq.lib.chat_completion_chunk import ChatCompletionChunk
|
| 44 |
-
from groq.resources import Models
|
| 45 |
-
from groq.types import ModelList
|
| 46 |
-
from groq.types.chat.completion_create_params import Message
|
| 47 |
-
|
| 48 |
-
import async_timeout
|
| 49 |
-
import asyncio
|
| 50 |
-
from interpreter import interpreter
|
| 51 |
-
import os
|
| 52 |
-
|
| 53 |
-
GENERATION_TIMEOUT_SEC = 60
|
| 54 |
-
|
| 55 |
-
if __name__ == "__main__":
|
| 56 |
-
import sys
|
| 57 |
|
| 58 |
-
|
| 59 |
-
print(f"🔍 sys.argv: {sys.argv}")
|
| 60 |
-
print(f"🔍 SPACE_ID環境変数: {os.getenv('SPACE_ID')}")
|
| 61 |
-
print(f"🔍 '--gradio' in sys.argv: {'--gradio' in sys.argv}")
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# デバッグモード: reloadを無効にしてブレークポイントを使用可能に
|
| 85 |
-
uvicorn.run(
|
| 86 |
-
"mysite.asgi:app",
|
| 87 |
-
host="0.0.0.0",
|
| 88 |
-
port=7860,
|
| 89 |
-
reload=False, # デバッグ時はリロード無効
|
| 90 |
-
log_level="debug",
|
| 91 |
-
access_log=True,
|
| 92 |
-
use_colors=True
|
| 93 |
-
)
|
| 94 |
-
else:
|
| 95 |
-
print("📍 開発モード: ホットリロードが有効です")
|
| 96 |
-
# 開発モード: reloadを有効にして高速開発
|
| 97 |
-
uvicorn.run(
|
| 98 |
-
"mysite.asgi:app",
|
| 99 |
-
host="0.0.0.0",
|
| 100 |
-
port=7860,
|
| 101 |
-
reload=True, # 開発時はリロード有効
|
| 102 |
-
log_level="debug",
|
| 103 |
-
access_log=True,
|
| 104 |
-
use_colors=True,
|
| 105 |
-
reload_dirs=["/workspaces/fastapi_django_main_live"]
|
| 106 |
-
)
|
| 107 |
-
|
| 108 |
-
except Exception as e:
|
| 109 |
-
print(f"❌ アプリケーション起��エラー: {e}")
|
| 110 |
-
import traceback
|
| 111 |
-
traceback.print_exc()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
|
|
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
|
| 6 |
# .envファイルから環境変数を読み込み
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
+
def greet(name):
|
| 10 |
+
"""簡単な挨拶関数"""
|
| 11 |
+
if name:
|
| 12 |
+
return f"🚀 Hello {name}! Welcome to FastAPI Django Main Live on Hugging Face Spaces!"
|
| 13 |
+
return "👋 Welcome to FastAPI Django Main Live!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def system_info():
|
| 16 |
+
"""システム情報を表示"""
|
| 17 |
+
info = f"""
|
| 18 |
+
### 🤗 Hugging Face Spaces
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
**Environment:** {os.getenv('SPACE_ID', 'Local')}
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
**Features:**
|
| 23 |
+
- FastAPI Integration
|
| 24 |
+
- Django Backend
|
| 25 |
+
- Gradio UI
|
| 26 |
+
- AI Powered
|
| 27 |
|
| 28 |
+
🎯 This is a Gradio interface for the FastAPI Django application.
|
| 29 |
+
"""
|
| 30 |
+
return info
|
| 31 |
+
|
| 32 |
+
# Gradioインターフェースを作成
|
| 33 |
+
with gr.Blocks(title="FastAPI Django Main Live", theme=gr.themes.Soft()) as demo:
|
| 34 |
+
gr.Markdown("# 🚀 FastAPI Django Main Live")
|
| 35 |
+
gr.Markdown("### Hugging Face Spaces Demo")
|
| 36 |
|
| 37 |
+
with gr.Tab("Welcome"):
|
| 38 |
+
name_input = gr.Textbox(label="Your Name", placeholder="Enter your name...")
|
| 39 |
+
greet_button = gr.Button("Greet Me!")
|
| 40 |
+
output = gr.Textbox(label="Message")
|
| 41 |
+
greet_button.click(greet, inputs=name_input, outputs=output)
|
| 42 |
|
| 43 |
+
with gr.Tab("System Info"):
|
| 44 |
+
info_button = gr.Button("Show System Info")
|
| 45 |
+
info_output = gr.Markdown()
|
| 46 |
+
info_button.click(system_info, outputs=info_output)
|
| 47 |
+
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
print("🚀 Starting Gradio app on Hugging Face Spaces...")
|
| 50 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,61 +1,3 @@
|
|
| 1 |
-
# Core dependencies
|
| 2 |
-
fastapi==0.110.0
|
| 3 |
-
uvicorn==0.27.1
|
| 4 |
gradio==4.31.5
|
| 5 |
-
starlette==0.36.3
|
| 6 |
-
pydantic==2.6.2
|
| 7 |
-
pydantic-core==2.16.3
|
| 8 |
-
|
| 9 |
-
# Django
|
| 10 |
-
django==5.0.4
|
| 11 |
-
asgiref==3.8.1
|
| 12 |
-
sqlparse==0.5.0
|
| 13 |
-
whitenoise==6.6.0
|
| 14 |
-
|
| 15 |
-
# AI & ML
|
| 16 |
-
open-interpreter
|
| 17 |
-
groq==0.4.1
|
| 18 |
-
llamafactory
|
| 19 |
-
accelerate
|
| 20 |
-
diffusers
|
| 21 |
-
|
| 22 |
-
# Database
|
| 23 |
-
duckdb
|
| 24 |
-
psycopg2-binary
|
| 25 |
-
|
| 26 |
-
# Utilities
|
| 27 |
python-dotenv
|
| 28 |
-
jinja2
|
| 29 |
-
aiofiles
|
| 30 |
-
click==8.1.7
|
| 31 |
-
annotated-types==0.6.0
|
| 32 |
-
anyio==4.3.0
|
| 33 |
-
async-timeout==4.0.3
|
| 34 |
-
certifi==2024.2.2
|
| 35 |
-
distro==1.9.0
|
| 36 |
-
h11==0.14.0
|
| 37 |
-
httpcore==1.0.4
|
| 38 |
-
httpx==0.27.0
|
| 39 |
-
idna==3.6
|
| 40 |
-
sniffio==1.3.1
|
| 41 |
-
sse-starlette==2.0.0
|
| 42 |
-
typing-extensions==4.10.0
|
| 43 |
-
colorama==0.4.6
|
| 44 |
-
exceptiongroup==1.2.0
|
| 45 |
-
tzdata==2024.1
|
| 46 |
-
uvloop==0.19.0
|
| 47 |
-
huggingface-hub
|
| 48 |
-
imageio[ffmpeg]
|
| 49 |
-
torch
|
| 50 |
-
torchvision
|
| 51 |
-
transformers>=4.41.2
|
| 52 |
-
langchain
|
| 53 |
-
langchain_groq
|
| 54 |
-
sqlalchemy
|
| 55 |
-
sentence-transformers
|
| 56 |
-
google-auth
|
| 57 |
-
google-auth-oauthlib
|
| 58 |
-
google-auth-httplib2
|
| 59 |
-
google-api-python-client
|
| 60 |
-
line-bot-sdk
|
| 61 |
-
gradio_client
|
|
|
|
| 1 |
+
# Core dependencies for Gradio on HF Spaces
|
|
|
|
|
|
|
| 2 |
gradio==4.31.5
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
python-dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|