Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,6 @@ from dotenv import load_dotenv
|
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
-
# دریافت کلید از تنظیمات اسپیس
|
| 12 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 13 |
|
| 14 |
app = FastAPI()
|
|
@@ -18,32 +17,49 @@ class IdeaRequest(BaseModel):
|
|
| 18 |
|
| 19 |
if api_key:
|
| 20 |
genai.configure(api_key=api_key)
|
|
|
|
| 21 |
model = genai.GenerativeModel('gemini-2.5-flash')
|
| 22 |
|
| 23 |
@app.get("/", response_class=HTMLResponse)
|
| 24 |
async def read_root():
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
@app.post("/api/refine")
|
| 29 |
async def refine_text(request: IdeaRequest):
|
| 30 |
if not api_key:
|
| 31 |
-
raise HTTPException(status_code=500, detail="کلید API
|
| 32 |
|
|
|
|
| 33 |
prompt = f"""
|
| 34 |
-
You are
|
| 35 |
-
|
| 36 |
-
2. Lyrics based on the topic.
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
Output strictly in JSON format:
|
| 44 |
{{
|
| 45 |
-
"music_prompt": "
|
| 46 |
-
"lyrics": "
|
| 47 |
}}
|
| 48 |
|
| 49 |
User Input: {request.idea}
|
|
@@ -51,7 +67,6 @@ async def refine_text(request: IdeaRequest):
|
|
| 51 |
|
| 52 |
try:
|
| 53 |
result = model.generate_content(prompt)
|
| 54 |
-
# تمیزکاری جیسون
|
| 55 |
clean_json = result.text.replace("```json", "").replace("```", "").strip()
|
| 56 |
data = json.loads(clean_json)
|
| 57 |
return JSONResponse(content=data)
|
|
|
|
| 8 |
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
| 11 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 12 |
|
| 13 |
app = FastAPI()
|
|
|
|
| 17 |
|
| 18 |
if api_key:
|
| 19 |
genai.configure(api_key=api_key)
|
| 20 |
+
# استفاده از مدل فلش برای سرعت و دقت بالا
|
| 21 |
model = genai.GenerativeModel('gemini-2.5-flash')
|
| 22 |
|
| 23 |
@app.get("/", response_class=HTMLResponse)
|
| 24 |
async def read_root():
|
| 25 |
+
try:
|
| 26 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
| 27 |
+
return f.read()
|
| 28 |
+
except FileNotFoundError:
|
| 29 |
+
return "<h1>Error: index.html not found</h1>"
|
| 30 |
|
| 31 |
@app.post("/api/refine")
|
| 32 |
async def refine_text(request: IdeaRequest):
|
| 33 |
if not api_key:
|
| 34 |
+
raise HTTPException(status_code=500, detail="کلید API تنظیم نشده است.")
|
| 35 |
|
| 36 |
+
# پرامپت دقیق برای تولید آهنگ طولانی و ساختار یافته
|
| 37 |
prompt = f"""
|
| 38 |
+
You are a professional songwriter and music producer.
|
| 39 |
+
Task: Convert the user's input into a COMPLETE song structure and a music generation prompt.
|
|
|
|
| 40 |
|
| 41 |
+
1. **Music Prompt (English):** Describe the mood, instruments, BPM, and style suitable for an AI music generator.
|
| 42 |
+
|
| 43 |
+
2. **Lyrics (Persian/Farsi):**
|
| 44 |
+
- The song MUST be long enough for **3 to 5 minutes**.
|
| 45 |
+
- Do **NOT** use diacritics (اعراب نگذار). Write standard Persian.
|
| 46 |
+
- You MUST use the following structure tags exactly:
|
| 47 |
+
[Intro]
|
| 48 |
+
[Verse 1]
|
| 49 |
+
[Pre-Chorus]
|
| 50 |
+
[Chorus]
|
| 51 |
+
[Verse 2]
|
| 52 |
+
[Chorus]
|
| 53 |
+
[Bridge]
|
| 54 |
+
[Guitar Solo] (or Instrumental Break)
|
| 55 |
+
[Chorus]
|
| 56 |
+
[Outro]
|
| 57 |
+
- Ensure rhymes and rhythm are professional.
|
| 58 |
|
| 59 |
Output strictly in JSON format:
|
| 60 |
{{
|
| 61 |
+
"music_prompt": "YOUR ENGLISH PROMPT HERE",
|
| 62 |
+
"lyrics": "YOUR FULL LYRICS HERE"
|
| 63 |
}}
|
| 64 |
|
| 65 |
User Input: {request.idea}
|
|
|
|
| 67 |
|
| 68 |
try:
|
| 69 |
result = model.generate_content(prompt)
|
|
|
|
| 70 |
clean_json = result.text.replace("```json", "").replace("```", "").strip()
|
| 71 |
data = json.loads(clean_json)
|
| 72 |
return JSONResponse(content=data)
|