Hardik5456 commited on
Commit
5132af6
·
verified ·
1 Parent(s): 77949b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -45
app.py CHANGED
@@ -1,46 +1,44 @@
 
1
  import requests
2
- import json
3
-
4
- # Replace with your actual API endpoint and API key
5
- API_URL = "https://api.nvidia.com/llama-3.1-nemotron-ultra-253b-v1"
6
- API_KEY = "your_api_key_here"
7
-
8
- # Your cinematic scene prompt
9
- cinematic_prompt = """
10
- INT. ANCIENT MARATHA FORT - NIGHT
11
-
12
- The rain lashes against the stone walls as SAMBHAJI MAHARAJ, regal and fierce, stands silhouetted by lightning atop the ramparts. His eyes burn with defiance. Thunder cracks. Below, a vast Mughal army advances. The drums of war echo in the mist. SAMBHAJI slowly unsheathes his sword — engraved with ancient Maratha inscriptions.
13
-
14
- CAMERA: Low-angle, slow dolly-in as wind catches his cape.
15
- LIGHTING: Moonlit with flickering torches, rain reflections.
16
- SFX: Thunder, horses in distance, war drums.
17
-
18
- [Generate scene narration in immersive cinematic prose.]
19
- """
20
-
21
- # Payload for the API
22
- payload = {
23
- "model": "llama-3.1-nemotron-ultra-253b-v1",
24
- "prompt": cinematic_prompt,
25
- "temperature": 0.7,
26
- "top_p": 0.9,
27
- "max_tokens": 1024,
28
- "stop": ["\n\n", "[END]"]
29
- }
30
-
31
- # Headers
32
- headers = {
33
- "Content-Type": "application/json",
34
- "Authorization": f"Bearer {API_KEY}"
35
- }
36
-
37
- # Safe API Call
38
- try:
39
- response = requests.post(API_URL, json=payload, headers=headers)
40
- response.raise_for_status() # Raise error for non-2xx responses
41
- data = response.json()
42
- print("Generated Output:\n", data.get("text", data)) # Adjust based on actual response format
43
- except requests.exceptions.RequestException as e:
44
- print("API Request failed:", str(e))
45
- except json.JSONDecodeError as e:
46
- print("JSON Decode Error:", str(e))
 
1
+ import os
2
  import requests
3
+ import gradio as gr
4
+
5
+ # Retrieve the NVIDIA API key from Hugging Face Space secrets
6
+ NVIDIA_API_KEY = os.getenv("NVIDIA_API_KEY")
7
+ if not NVIDIA_API_KEY:
8
+ raise ValueError("NVIDIA_API_KEY not found in environment variables")
9
+
10
+ # Function to generate a response using the NVIDIA API
11
+ def generate_response(input_text):
12
+ # Placeholder NVIDIA API endpoint (replace with the actual endpoint)
13
+ url = "https://api.nvidia.com/v1/models/llama-3.1-nemotron-ultra-253b-v1/generate"
14
+ headers = {
15
+ "Authorization": f"Bearer {NVIDIA_API_KEY}",
16
+ "Content-Type": "application/json",
17
+ }
18
+ data = {
19
+ "prompt": input_text,
20
+ "max_tokens": 100, # Adjust as needed
21
+ "temperature": 0.7, # Adjust for creativity/control
22
+ # Add other parameters as required by the NVIDIA API
23
+ }
24
+ try:
25
+ response = requests.post(url, headers=headers, json=data)
26
+ response.raise_for_status() # Raise an exception for HTTP errors
27
+ output = response.json()
28
+ # Adjust the key based on the actual API response format
29
+ return output["generated_text"]
30
+ except requests.exceptions.RequestException as e:
31
+ return f"Error: {str(e)}"
32
+
33
+ # Set up the Gradio interface
34
+ iface = gr.Interface(
35
+ fn=generate_response,
36
+ inputs=gr.Textbox(label="Input your query"),
37
+ outputs=gr.Textbox(label="Response"),
38
+ title="Llama 3.1 Nemotron Ultra 253B v1",
39
+ description="Ask a question and get a response from the AI model."
40
+ )
41
+
42
+ # Launch the app
43
+ if __name__ == "__main__":
44
+ iface.launch()