Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import whisper
|
3 |
+
import requests
|
4 |
+
from flask import Flask, request, jsonify, render_template
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
from deepgram import DeepgramClient, PrerecordedOptions
|
7 |
+
import tempfile
|
8 |
+
import json
|
9 |
+
import subprocess
|
10 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
11 |
+
|
12 |
+
|
13 |
+
import warnings
|
14 |
+
warnings.filterwarnings("ignore", message="FP16 is not supported on CPU; using FP32 instead")
|
15 |
+
|
16 |
+
app = Flask(__name__)
|
17 |
+
print("APP IS RUNNING, ANIKET")
|
18 |
+
|
19 |
+
# Load the .env file
|
20 |
+
load_dotenv()
|
21 |
+
|
22 |
+
print("ENV LOADED, ANIKET")
|
23 |
+
|
24 |
+
# Fetch the API key from the .env file
|
25 |
+
API_KEY = os.getenv("FIRST_API_KEY")
|
26 |
+
DEEPGRAM_API_KEY = os.getenv("SECOND_API_KEY")
|
27 |
+
|
28 |
+
# Ensure the API key is loaded correctly
|
29 |
+
if not API_KEY:
|
30 |
+
raise ValueError("API Key not found. Make sure it is set in the .env file.")
|
31 |
+
|
32 |
+
if not DEEPGRAM_API_KEY:
|
33 |
+
raise ValueError("DEEPGRAM_API_KEY not found. Make sure it is set in the .env file.")
|
34 |
+
|
35 |
+
GEMINI_API_ENDPOINT = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent"
|
36 |
+
GEMINI_API_KEY = API_KEY
|
37 |
+
|
38 |
+
@app.route("/", methods=["GET"])
|
39 |
+
def health_check():
|
40 |
+
return jsonify({"status": "success", "message": "API is running successfully!"}), 200
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
def query_gemini_api(transcription):
|
45 |
+
"""
|
46 |
+
Send transcription text to Gemini API and fetch structured recipe information synchronously.
|
47 |
+
"""
|
48 |
+
try:
|
49 |
+
# Define the structured prompt
|
50 |
+
prompt = (
|
51 |
+
"Analyze the provided cooking video transcription and extract the following structured information:\n"
|
52 |
+
"1. Recipe Name: Identify the name of the dish being prepared.\n"
|
53 |
+
"2. Ingredients List: Extract a detailed list of ingredients with their respective quantities (if mentioned).\n"
|
54 |
+
"3. Steps for Preparation: Provide a step-by-step breakdown of the recipe's preparation process, organized and numbered sequentially.\n"
|
55 |
+
"4. Cooking Techniques Used: Highlight the cooking techniques demonstrated in the video, such as searing, blitzing, wrapping, etc.\n"
|
56 |
+
"5. Equipment Needed: List all tools, appliances, or utensils mentioned, e.g., blender, hot pan, cling film, etc.\n"
|
57 |
+
"6. Nutritional Information (if inferred): Provide an approximate calorie count or nutritional breakdown based on the ingredients used.\n"
|
58 |
+
"7. Serving size: In count of people or portion size.\n"
|
59 |
+
"8. Special Notes or Variations: Include any specific tips, variations, or alternatives mentioned.\n"
|
60 |
+
"9. Festive or Thematic Relevance: Note if the recipe has any special relevance to holidays, events, or seasons.\n"
|
61 |
+
f"Text: {transcription}\n"
|
62 |
+
)
|
63 |
+
|
64 |
+
# Prepare the payload and headers
|
65 |
+
payload = {
|
66 |
+
"contents": [
|
67 |
+
{
|
68 |
+
"parts": [
|
69 |
+
{"text": prompt}
|
70 |
+
]
|
71 |
+
}
|
72 |
+
]
|
73 |
+
}
|
74 |
+
headers = {"Content-Type": "application/json"}
|
75 |
+
|
76 |
+
# Send request to Gemini API synchronously
|
77 |
+
response = requests.post(
|
78 |
+
f"{GEMINI_API_ENDPOINT}?key={GEMINI_API_KEY}",
|
79 |
+
json=payload,
|
80 |
+
headers=headers,
|
81 |
+
)
|
82 |
+
|
83 |
+
# Raise error if response code is not 200
|
84 |
+
response.raise_for_status()
|
85 |
+
|
86 |
+
data = response.json()
|
87 |
+
|
88 |
+
return data.get("candidates", [{}])[0].get("content", {}).get("parts", [{}])[0].get("text", "No result found")
|
89 |
+
|
90 |
+
except requests.exceptions.RequestException as e:
|
91 |
+
print(f"Error querying Gemini API: {e}")
|
92 |
+
return {"error": str(e)}
|
93 |
+
|
94 |
+
|
95 |
+
if __name__ == '__main__':
|
96 |
+
app.run(debug=True)
|
97 |
+
|