Arielboim commited on
Commit
729a59d
1 Parent(s): 5b66dd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -1,39 +1,52 @@
1
  import gradio as gr
2
- import logging
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
- # Load the model
10
- logger.info("Loading the model...")
11
- model = gr.load("models/mattshumer/Reflection-Llama-3.1-70B")
12
- logger.info(f"Model loaded. Type: {type(model)}")
 
 
 
 
 
 
 
13
 
14
  def generate_text(prompt):
15
  logger.info(f"Received prompt: {prompt}")
16
 
17
  try:
18
  # Generate text
19
- logger.info("Calling model for text generation...")
20
- response = model(prompt)
21
- logger.info(f"Raw response from model: {response}")
22
-
23
- if isinstance(response, tuple):
24
- logger.info("Response is a tuple. Unpacking...")
25
- response = response[0] if response else "Empty response"
26
-
27
- if isinstance(response, dict):
28
- logger.info("Response is a dictionary. Extracting relevant information...")
29
- response = json.dumps(response, indent=2)
30
 
31
- if not isinstance(response, str):
32
- logger.warning(f"Unexpected response type: {type(response)}")
33
- response = str(response)
34
 
35
- logger.info(f"Processed response: {response[:100]}...") # Log first 100 chars
36
- return response
 
 
 
 
 
 
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)}"