Update app.py
Browse files
app.py
CHANGED
@@ -46,6 +46,21 @@ def get_client():
|
|
46 |
CLIENT, STATUS = get_client()
|
47 |
logger.info(f"Client status: {STATUS}")
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
50 |
# π€ SIMPLE TASK FUNCTIONS (No Async)
|
51 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
@@ -82,7 +97,7 @@ def speech_recognition(audio_file):
|
|
82 |
return f"β Error: {str(e)}"
|
83 |
|
84 |
def chat_completion(messages_json):
|
85 |
-
"""Chat Completion -
|
86 |
if not messages_json.strip():
|
87 |
return "β Please enter valid JSON messages"
|
88 |
|
@@ -92,9 +107,9 @@ def chat_completion(messages_json):
|
|
92 |
try:
|
93 |
messages = json.loads(messages_json)
|
94 |
|
95 |
-
#
|
96 |
completion = CLIENT.chat.completions.create(
|
97 |
-
model="
|
98 |
messages=messages,
|
99 |
)
|
100 |
|
@@ -103,10 +118,10 @@ def chat_completion(messages_json):
|
|
103 |
except json.JSONDecodeError:
|
104 |
return "β Invalid JSON format"
|
105 |
except Exception as e:
|
106 |
-
return f"β Error: {str(e)}"
|
107 |
|
108 |
def fill_mask(text):
|
109 |
-
"""Fill Mask - Following official docs exactly."""
|
110 |
if not text or "[MASK]" not in text:
|
111 |
return "β Text must contain [MASK] token"
|
112 |
|
@@ -114,12 +129,16 @@ def fill_mask(text):
|
|
114 |
return f"β Client not available: {STATUS}"
|
115 |
|
116 |
try:
|
|
|
|
|
117 |
# Official API call from docs
|
118 |
result = CLIENT.fill_mask(
|
119 |
text,
|
120 |
model="google-bert/bert-base-uncased",
|
121 |
)
|
122 |
|
|
|
|
|
123 |
if isinstance(result, list):
|
124 |
predictions = []
|
125 |
for i, pred in enumerate(result[:5], 1):
|
@@ -131,7 +150,8 @@ def fill_mask(text):
|
|
131 |
return f"π **Result:** {result}"
|
132 |
|
133 |
except Exception as e:
|
134 |
-
|
|
|
135 |
|
136 |
def question_answering(question, context):
|
137 |
"""Question Answering - Following official docs exactly."""
|
@@ -180,7 +200,7 @@ def summarization(text):
|
|
180 |
return f"β Error: {str(e)}"
|
181 |
|
182 |
def text_generation(prompt):
|
183 |
-
"""Text Generation -
|
184 |
if not prompt.strip():
|
185 |
return "β Prompt cannot be empty"
|
186 |
|
@@ -188,16 +208,16 @@ def text_generation(prompt):
|
|
188 |
return f"β Client not available: {STATUS}"
|
189 |
|
190 |
try:
|
191 |
-
#
|
192 |
completion = CLIENT.chat.completions.create(
|
193 |
-
model="
|
194 |
messages=[{"role": "user", "content": prompt}],
|
195 |
)
|
196 |
|
197 |
return f"βοΈ **Generated:** {completion.choices[0].message.content}"
|
198 |
|
199 |
except Exception as e:
|
200 |
-
return f"β Error: {str(e)}"
|
201 |
|
202 |
def image_classification(image_path):
|
203 |
"""Image Classification - Following official docs exactly."""
|
|
|
46 |
CLIENT, STATUS = get_client()
|
47 |
logger.info(f"Client status: {STATUS}")
|
48 |
|
49 |
+
# Add debug info
|
50 |
+
if CLIENT:
|
51 |
+
logger.info("β
Client successfully initialized with provider='hf-inference'")
|
52 |
+
|
53 |
+
# Test the client with a simple call
|
54 |
+
try:
|
55 |
+
test_result = CLIENT.fill_mask("The capital of France is [MASK].", model="google-bert/bert-base-uncased")
|
56 |
+
logger.info(f"β
Client test successful: {type(test_result)}")
|
57 |
+
except Exception as e:
|
58 |
+
logger.error(f"β Client test failed: {e}")
|
59 |
+
STATUS = f"Client test failed: {e}"
|
60 |
+
CLIENT = None
|
61 |
+
else:
|
62 |
+
logger.error(f"β Client initialization failed: {STATUS}")
|
63 |
+
|
64 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
65 |
# π€ SIMPLE TASK FUNCTIONS (No Async)
|
66 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
97 |
return f"β Error: {str(e)}"
|
98 |
|
99 |
def chat_completion(messages_json):
|
100 |
+
"""Chat Completion - Using exact working model from your test."""
|
101 |
if not messages_json.strip():
|
102 |
return "β Please enter valid JSON messages"
|
103 |
|
|
|
107 |
try:
|
108 |
messages = json.loads(messages_json)
|
109 |
|
110 |
+
# Using the EXACT same model and call that works in your test
|
111 |
completion = CLIENT.chat.completions.create(
|
112 |
+
model="Qwen/Qwen2.5-72B-Instruct", # Using available Qwen model
|
113 |
messages=messages,
|
114 |
)
|
115 |
|
|
|
118 |
except json.JSONDecodeError:
|
119 |
return "β Invalid JSON format"
|
120 |
except Exception as e:
|
121 |
+
return f"β Error: {str(e)}\n\n**Debug:** Model: Qwen/Qwen2.5-72B-Instruct, Messages: {messages}"
|
122 |
|
123 |
def fill_mask(text):
|
124 |
+
"""Fill Mask - Following official docs exactly with better debugging."""
|
125 |
if not text or "[MASK]" not in text:
|
126 |
return "β Text must contain [MASK] token"
|
127 |
|
|
|
129 |
return f"β Client not available: {STATUS}"
|
130 |
|
131 |
try:
|
132 |
+
logger.info(f"Making fill_mask call with text: {text}")
|
133 |
+
|
134 |
# Official API call from docs
|
135 |
result = CLIENT.fill_mask(
|
136 |
text,
|
137 |
model="google-bert/bert-base-uncased",
|
138 |
)
|
139 |
|
140 |
+
logger.info(f"fill_mask result type: {type(result)}, content: {result}")
|
141 |
+
|
142 |
if isinstance(result, list):
|
143 |
predictions = []
|
144 |
for i, pred in enumerate(result[:5], 1):
|
|
|
150 |
return f"π **Result:** {result}"
|
151 |
|
152 |
except Exception as e:
|
153 |
+
logger.error(f"fill_mask error: {e}")
|
154 |
+
return f"β Error: {str(e)}\n\n**Debug:** Model: google-bert/bert-base-uncased, Text: {text}"
|
155 |
|
156 |
def question_answering(question, context):
|
157 |
"""Question Answering - Following official docs exactly."""
|
|
|
200 |
return f"β Error: {str(e)}"
|
201 |
|
202 |
def text_generation(prompt):
|
203 |
+
"""Text Generation - Using working Qwen model."""
|
204 |
if not prompt.strip():
|
205 |
return "β Prompt cannot be empty"
|
206 |
|
|
|
208 |
return f"β Client not available: {STATUS}"
|
209 |
|
210 |
try:
|
211 |
+
# Using the same model family that works in your test
|
212 |
completion = CLIENT.chat.completions.create(
|
213 |
+
model="Qwen/Qwen2.5-72B-Instruct",
|
214 |
messages=[{"role": "user", "content": prompt}],
|
215 |
)
|
216 |
|
217 |
return f"βοΈ **Generated:** {completion.choices[0].message.content}"
|
218 |
|
219 |
except Exception as e:
|
220 |
+
return f"β Error: {str(e)}\n\n**Debug:** Model: Qwen/Qwen2.5-72B-Instruct, Prompt: {prompt}"
|
221 |
|
222 |
def image_classification(image_path):
|
223 |
"""Image Classification - Following official docs exactly."""
|