AkashKumarave commited on
Commit
efaf7fc
·
verified ·
1 Parent(s): 7ada8d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -2,15 +2,14 @@ import gradio as gr
2
  import requests
3
  import os
4
  from pathlib import Path
5
- import time
6
 
7
- # Kling AI API configuration
8
- KLING_API_URL = "https://api.klingai.com/v1/video" # Hypothetical endpoint; replace with actual Kling AI API endpoint
9
- KLING_SECRET_KEY = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde" # Provided secret key
10
 
11
  def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
12
  """
13
- Function to call Kling AI API for text-to-video generation.
14
 
15
  Args:
16
  prompt (str): Text prompt for video generation
@@ -21,6 +20,9 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
21
  Returns:
22
  str: Path to the downloaded video or error message
23
  """
 
 
 
24
  headers = {
25
  "Authorization": f"Bearer {KLING_SECRET_KEY}",
26
  "Content-Type": "application/json"
@@ -36,9 +38,9 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
36
  try:
37
  # Make API request to Kling AI
38
  response = requests.post(KLING_API_URL, json=payload, headers=headers, timeout=60)
39
- response.raise_for_status() # Raise exception for bad status codes
40
 
41
- # Assuming the API returns a JSON with a video URL or file
42
  data = response.json()
43
  video_url = data.get("video_url") # Adjust based on actual API response structure
44
 
@@ -49,7 +51,7 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
49
  video_response = requests.get(video_url, timeout=30)
50
  video_response.raise_for_status()
51
 
52
- # Save the video to a temporary file
53
  output_path = Path("output_video.mp4")
54
  with open(output_path, "wb") as f:
55
  f.write(video_response.content)
@@ -61,7 +63,7 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
61
 
62
  def chatbot_interface(prompt, negative_prompt, duration, aspect_ratio):
63
  """
64
- Gradio interface function to handle user inputs and display results.
65
 
66
  Args:
67
  prompt (str): Text prompt for video generation
@@ -92,6 +94,6 @@ iface = gr.Interface(
92
  description="Generate videos using Kling AI API. Enter a prompt, optionally a negative prompt, select duration and aspect ratio, and get your video!"
93
  )
94
 
95
- # Launch the interface
96
  if __name__ == "__main__":
97
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
2
  import requests
3
  import os
4
  from pathlib import Path
 
5
 
6
+ # Retrieve Kling AI API key from environment variable (set as a Space secret)
7
+ KLING_SECRET_KEY = os.environ.get("KLING_SECRET_KEY")
8
+ KLING_API_URL = "https://api.klingai.com/v1/video" # Replace with actual Kling AI API endpoint
9
 
10
  def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
11
  """
12
+ Call Kling AI API to generate a video based on a text prompt.
13
 
14
  Args:
15
  prompt (str): Text prompt for video generation
 
20
  Returns:
21
  str: Path to the downloaded video or error message
22
  """
23
+ if not KLING_SECRET_KEY:
24
+ return "Error: KLING_SECRET_KEY environment variable not set."
25
+
26
  headers = {
27
  "Authorization": f"Bearer {KLING_SECRET_KEY}",
28
  "Content-Type": "application/json"
 
38
  try:
39
  # Make API request to Kling AI
40
  response = requests.post(KLING_API_URL, json=payload, headers=headers, timeout=60)
41
+ response.raise_for_status()
42
 
43
+ # Assuming API returns a JSON with a video URL
44
  data = response.json()
45
  video_url = data.get("video_url") # Adjust based on actual API response structure
46
 
 
51
  video_response = requests.get(video_url, timeout=30)
52
  video_response.raise_for_status()
53
 
54
+ # Save video to a temporary file
55
  output_path = Path("output_video.mp4")
56
  with open(output_path, "wb") as f:
57
  f.write(video_response.content)
 
63
 
64
  def chatbot_interface(prompt, negative_prompt, duration, aspect_ratio):
65
  """
66
+ Gradio interface to handle user inputs and display results.
67
 
68
  Args:
69
  prompt (str): Text prompt for video generation
 
94
  description="Generate videos using Kling AI API. Enter a prompt, optionally a negative prompt, select duration and aspect ratio, and get your video!"
95
  )
96
 
97
+ # Launch the interface (Hugging Face Spaces handles this automatically)
98
  if __name__ == "__main__":
99
+ iface.launch()