satishsnv commited on
Commit
a272826
Β·
1 Parent(s): 61c663e

fixed label

Browse files
Files changed (2) hide show
  1. app.py +4 -4
  2. test.py +58 -13
app.py CHANGED
@@ -115,7 +115,7 @@ def summarize_and_analyze_sentiment(api_key: str, content: str) -> tuple:
115
  if line.startswith('SUMMARY:'):
116
  summary = line.replace('SUMMARY:', '').strip()
117
  elif line.startswith('SENTIMENT_LABEL:'):
118
- sentiment_label = line.replace('SENTIMENT_LABEL:', '').strip().lower()
119
  elif line.startswith('SENTIMENT_SCORE:'):
120
  try:
121
  sentiment_score = float(line.replace('SENTIMENT_SCORE:', '').strip())
@@ -125,14 +125,14 @@ def summarize_and_analyze_sentiment(api_key: str, content: str) -> tuple:
125
  sentiment_score = 0.0
126
 
127
  # Ensure valid sentiment label
128
- if sentiment_label not in ['positive', 'negative']:
129
- sentiment_label = 'positive' if sentiment_score > 0 else 'negative'
130
 
131
  return summary, {"label": sentiment_label, "score": sentiment_score}
132
 
133
  except Exception as e:
134
  logger.error(f"Summarization error: {str(e)}")
135
- return "Error generating summary", {"label": "neutral", "score": 0.0}
136
 
137
  @app.get("/")
138
  async def root():
 
115
  if line.startswith('SUMMARY:'):
116
  summary = line.replace('SUMMARY:', '').strip()
117
  elif line.startswith('SENTIMENT_LABEL:'):
118
+ sentiment_label = line.replace('SENTIMENT_LABEL:', '').strip()
119
  elif line.startswith('SENTIMENT_SCORE:'):
120
  try:
121
  sentiment_score = float(line.replace('SENTIMENT_SCORE:', '').strip())
 
125
  sentiment_score = 0.0
126
 
127
  # Ensure valid sentiment label
128
+ if sentiment_label not in ['POSITIVE', 'NEGATIVE']:
129
+ sentiment_label = 'POSITIVE' if sentiment_score >= 0 else 'NEGATIVE'
130
 
131
  return summary, {"label": sentiment_label, "score": sentiment_score}
132
 
133
  except Exception as e:
134
  logger.error(f"Summarization error: {str(e)}")
135
+ return "Error generating summary"
136
 
137
  @app.get("/")
138
  async def root():
test.py CHANGED
@@ -12,28 +12,73 @@ from dotenv import load_dotenv
12
 
13
  load_dotenv() # Load environment variables from .env file
14
 
15
- # Possible Hugging Face Space URLs to try
16
-
 
 
 
 
 
 
 
17
 
18
  # Update this to your actual Hugging Face Space URL or use "local" for localhost:7860
19
- BASE_URL = "https://snvskiit/content_processor.hf.space" # Default to first URL
20
  # BASE_URL = "http://localhost:7860" # Uncomment for local testing
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def test_api():
23
  """Test the deployed API"""
 
24
 
25
  # Handle local testing
26
  if len(sys.argv) > 1 and sys.argv[1] == "local":
27
- global BASE_URL
28
  BASE_URL = "http://localhost:7860"
29
  print("🏠 Testing local server...")
 
 
 
 
 
 
 
 
 
30
  else:
31
  print("🌐 Testing Hugging Face Space deployment...")
32
- print("πŸ” If 404 errors occur, the Space might be:")
33
- print(" β€’ Not properly deployed")
34
- print(" β€’ Sleeping (try refreshing the Space URL in browser)")
35
- print(" β€’ Using a different URL")
36
- print(" β€’ Having deployment issues")
37
 
38
  # Get OpenAI API key from environment
39
  api_key = os.getenv("OPENAI_API_KEY")
@@ -123,16 +168,16 @@ def test_api():
123
 
124
  print(f"\nπŸ“– View docs at: {BASE_URL}/docs")
125
  print(f"\nπŸ’‘ Usage:")
126
- print(f" python test.py # Test Hugging Face deployment")
 
127
  print(f" python test.py local # Test local server")
128
 
129
  if "hf.space" in BASE_URL:
130
  print(f"\nπŸ”§ Troubleshooting Hugging Face Space:")
131
  print(f" β€’ Check if Space is active: {BASE_URL}")
132
- print(f" β€’ Try refreshing the Space in browser to wake it up")
133
  print(f" β€’ Check Space logs for deployment errors")
134
- print(f" β€’ Verify the Space URL is correct")
135
- print(f" β€’ Try alternative URLs:")
136
 
137
 
138
  if __name__ == "__main__":
 
12
 
13
  load_dotenv() # Load environment variables from .env file
14
 
15
+ # Possible Hugging Face Space URLs to try - Common formats for HF Spaces
16
+ POSSIBLE_URLS = [
17
+ "https://snvskiit-content-processor.hf.space", # Standard format: username-spacename
18
+ "https://snvskiit-content_processor.hf.space", # With underscore
19
+ "https://content-processor-snvskiit.hf.space", # Reverse order with dash
20
+ "https://content_processor-snvskiit.hf.space", # Reverse order with underscore
21
+ "https://huggingface.co/spaces/snvskiit/content-processor", # Direct HF URL with dash
22
+ "https://huggingface.co/spaces/snvskiit/content_processor", # Direct HF URL with underscore
23
+ ]
24
 
25
  # Update this to your actual Hugging Face Space URL or use "local" for localhost:7860
26
+ BASE_URL = "https://snvskiit-content-processor.hf.space" # Working endpoint!
27
  # BASE_URL = "http://localhost:7860" # Uncomment for local testing
28
 
29
+ def find_working_hf_endpoint():
30
+ """Try different HF Space URL formats to find the working one"""
31
+ print("πŸ” Searching for working Hugging Face Space endpoint...")
32
+ print("=" * 60)
33
+
34
+ for i, url in enumerate(POSSIBLE_URLS, 1):
35
+ print(f"\n{i}. Testing: {url}")
36
+ try:
37
+ with httpx.Client(timeout=10.0, verify=False) as client:
38
+ response = client.get(f"{url}/health")
39
+ if response.status_code == 200:
40
+ print(f" βœ… SUCCESS! Found working endpoint")
41
+ try:
42
+ data = response.json()
43
+ print(f" πŸ“Š Response: {data}")
44
+ return url
45
+ except:
46
+ print(f" πŸ“ Response (non-JSON): {response.text[:100]}...")
47
+ return url
48
+ elif response.status_code == 404:
49
+ print(f" ❌ 404 - Space not found at this URL")
50
+ else:
51
+ print(f" ⚠️ Status {response.status_code} - {response.text[:50]}...")
52
+ except Exception as e:
53
+ print(f" ❌ Connection failed: {str(e)[:50]}...")
54
+
55
+ print(f"\n🚫 No working endpoint found. Please check:")
56
+ print(f" β€’ Your Hugging Face username")
57
+ print(f" β€’ Your Space name")
58
+ print(f" β€’ That the Space is deployed and running")
59
+ return None
60
+
61
  def test_api():
62
  """Test the deployed API"""
63
+ global BASE_URL
64
 
65
  # Handle local testing
66
  if len(sys.argv) > 1 and sys.argv[1] == "local":
 
67
  BASE_URL = "http://localhost:7860"
68
  print("🏠 Testing local server...")
69
+ elif len(sys.argv) > 1 and sys.argv[1] == "find":
70
+ # Find working endpoint mode
71
+ working_url = find_working_hf_endpoint()
72
+ if working_url:
73
+ BASE_URL = working_url
74
+ print(f"\n🎯 Using found endpoint: {BASE_URL}")
75
+ else:
76
+ print(f"\n⚠️ Falling back to default: {BASE_URL}")
77
+ print("\n" + "=" * 60)
78
  else:
79
  print("🌐 Testing Hugging Face Space deployment...")
80
+ print("πŸ” If 404 errors occur, try: python test.py find")
81
+ print(" This will test all possible URL formats")
 
 
 
82
 
83
  # Get OpenAI API key from environment
84
  api_key = os.getenv("OPENAI_API_KEY")
 
168
 
169
  print(f"\nπŸ“– View docs at: {BASE_URL}/docs")
170
  print(f"\nπŸ’‘ Usage:")
171
+ print(f" python test.py # Test default Hugging Face deployment")
172
+ print(f" python test.py find # Auto-discover working HF endpoint")
173
  print(f" python test.py local # Test local server")
174
 
175
  if "hf.space" in BASE_URL:
176
  print(f"\nπŸ”§ Troubleshooting Hugging Face Space:")
177
  print(f" β€’ Check if Space is active: {BASE_URL}")
178
+ print(f" β€’ Try: python test.py find")
179
  print(f" β€’ Check Space logs for deployment errors")
180
+ print(f" β€’ Verify your username and space name are correct")
 
181
 
182
 
183
  if __name__ == "__main__":