Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
import json
|
|
|
|
|
4 |
|
5 |
# Set up logging
|
6 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
7 |
logger = logging.getLogger(__name__)
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def generate_text(prompt):
|
15 |
logger.info(f"Received prompt: {prompt}")
|
16 |
|
17 |
try:
|
18 |
# Generate text
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
logger.warning(f"Unexpected response type: {type(response)}")
|
33 |
-
response = str(response)
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
except Exception as e:
|
39 |
error_msg = f"Error generating text: {str(e)}"
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
import json
|
4 |
+
import logging
|
5 |
+
import os
|
6 |
|
7 |
# Set up logging
|
8 |
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
11 |
+
# Hugging Face API settings
|
12 |
+
API_URL = "https://api-inference.huggingface.co/models/mattshumer/Reflection-Llama-3.1-70B"
|
13 |
+
API_TOKEN = os.environ.get("HUGGINGFACE_API_TOKEN") # Make sure to set this in your Space's secrets
|
14 |
+
|
15 |
+
headers = {"Authorization": f"Bearer {API_TOKEN}"}
|
16 |
+
|
17 |
+
def query(payload):
|
18 |
+
logger.info(f"Sending payload to API: {payload}")
|
19 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
20 |
+
logger.info(f"Received response with status code: {response.status_code}")
|
21 |
+
return response.json()
|
22 |
|
23 |
def generate_text(prompt):
|
24 |
logger.info(f"Received prompt: {prompt}")
|
25 |
|
26 |
try:
|
27 |
# Generate text
|
28 |
+
payload = {
|
29 |
+
"inputs": prompt,
|
30 |
+
"parameters": {
|
31 |
+
"max_new_tokens": 100,
|
32 |
+
"temperature": 0.7,
|
33 |
+
"top_p": 0.95,
|
34 |
+
"do_sample": True
|
35 |
+
}
|
36 |
+
}
|
37 |
+
logger.info("Calling Hugging Face Inference API for text generation...")
|
38 |
+
response = query(payload)
|
39 |
|
40 |
+
logger.info(f"Raw response from API: {json.dumps(response, indent=2)}")
|
|
|
|
|
41 |
|
42 |
+
if isinstance(response, list) and len(response) > 0:
|
43 |
+
generated_text = response[0].get('generated_text', '')
|
44 |
+
logger.info(f"Processed response: {generated_text[:100]}...") # Log first 100 chars
|
45 |
+
return generated_text
|
46 |
+
else:
|
47 |
+
error_msg = f"Unexpected response format: {response}"
|
48 |
+
logger.error(error_msg)
|
49 |
+
return error_msg
|
50 |
|
51 |
except Exception as e:
|
52 |
error_msg = f"Error generating text: {str(e)}"
|