Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,44 @@
|
|
|
|
1 |
import requests
|
2 |
-
import
|
3 |
-
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
""
|
20 |
-
|
21 |
-
#
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
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()
|
|
|
|
|
|