Spaces:
Sleeping
Sleeping
Create pages/Check.py
Browse files- pages/Check.py +36 -0
pages/Check.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import os
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
|
5 |
+
load_dotenv()
|
6 |
+
|
7 |
+
# Define the endpoint and your API key
|
8 |
+
api_url = "https://api-inference.huggingface.co/models/meta-llama/Meta-Llama-3-8B-Instruct"
|
9 |
+
api_key = os.getenv('HFSecret')
|
10 |
+
|
11 |
+
# Define the headers for authorization
|
12 |
+
headers = {
|
13 |
+
"Authorization": f"Bearer {api_key}"
|
14 |
+
}
|
15 |
+
|
16 |
+
# Function to call the HuggingFace API
|
17 |
+
def call_huggingface_api(prompt):
|
18 |
+
data = {
|
19 |
+
"inputs": prompt,
|
20 |
+
"parameters": {"max_length": 500, "temperature": 0.5}
|
21 |
+
}
|
22 |
+
|
23 |
+
response = requests.post(api_url, headers=headers, json=data)
|
24 |
+
|
25 |
+
if response.status_code != 200:
|
26 |
+
raise Exception(f"Error: {response.status_code}, {response.text}")
|
27 |
+
|
28 |
+
return response.json()
|
29 |
+
|
30 |
+
# Example: Sentiment analysis
|
31 |
+
prompt = "Perform sentiment analysis on the following text: I love programming!"
|
32 |
+
try:
|
33 |
+
result = call_huggingface_api(prompt)
|
34 |
+
print(result)
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Error: {e}")
|