Spaces:
Build error
Build error
Ganesh Chintalapati commited on
Commit ·
64c8f1b
1
Parent(s): dca1617
fixing google2
Browse files180 unmodified lines181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219102 unmodified lines322323324325326327328180 unmodified lines) as response:response.raise_for_status()buffer = ""async for chunk in response.aiter_text():if chunk:buffer += chunk# Split buffer by newlines, but keep incomplete lines in bufferlines = buffer.split("\n")buffer = lines[-1] # Keep the last (potentially incomplete) linefor line in lines[:-1]:if not line.strip():continuelogger.info(f"Gemini stream chunk: {line}")try:json_data = json.loads(line)if isinstance(json_data, dict) and "candidates" in json_data and json_data["candidates"]:content = json_data["candidates"][0].get("content", {})if "parts" in content and content["parts"]:text = content["parts"][0].get("text", "")if text:yield textelif isinstance(json_data, list) and json_data:# Handle case where response is a list of JSON objectsfor item in json_data:if "candidates" in item and item["candidates"]:content = item["candidates"][0].get("content", {})if "parts" in content and content["parts"]:text = content["parts"][0].get("text", "")if text:yield textexcept json.JSONDecodeError as e:logger.error(f"Error parsing Gemini stream chunk: {str(e)} - Data: {line}")yield f"Error parsing stream: {str(e)}"except Exception as e:logger.error(f"Unexpected error in Gemini stream: {str(e)} - Data: {line}")yield f"Error in stream: {str(e)}"except httpx.HTTPStatusError as e:response_text = await e.response.aread()102 unmodified linesasync for response_chunk, openai_messages, anthropic_messages, gemini_messages in query_model(query, providers, history):yield "", openai_messages, anthropic_messages, gemini_messages# Final yield to clear the query textboxyield "", openai_messages, anthropic_messages, gemini_messages# Gradio interfacedef clear_history():180 unmodified lines181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239102 unmodified lines342343344345346347348180 unmodified lines) as response:response.raise_for_status()buffer = ""brace_count = 0bracket_count = 0in_string = Falseasync for chunk in response.aiter_text():if chunk:buffer += chunklogger.info(f"Gemini stream chunk: {chunk}")# Parse buffer for complete JSON objectsi = 0while i < len(buffer):char = buffer[i]if char == '"' and (i == 0 or buffer[i-1] != '\\'):in_string = not in_stringif not in_string:if char == '{':brace_count += 1elif char == '}':brace_count -= 1elif char == '[':bracket_count += 1elif char == ']':bracket_count -= 1i += 1# Check if we have a complete JSON objectif brace_count == 0 and bracket_count == 0 and not in_string and buffer.strip():try:json_data = json.loads(buffer)logger.info(f"Parsed Gemini JSON: {json_data}")buffer = "" # Reset buffer after parsingif isinstance(json_data, list):for item in json_data:if isinstance(item, dict) and "candidates" in item and item["candidates"]:content = item["candidates"][0].get("content", {})if "parts" in content and content["parts"]:text = content["parts"][0].get("text", "")if text:yield textelif isinstance(json_data, dict) and "candidates" in json_data and json_data["candidates"]:content = json_data["candidates"][0].get("content", {})if "parts" in content and content["parts"]:text = content["parts"][0].get("text", "")if text:yield textexcept json.JSONDecodeError as e:logger.error(f"Error parsing Gemini stream chunk: {str(e)} - Data: {buffer}")# Continue accumulating buffer if JSON is incompletebreakexcept Exception as e:logger.error(f"Unexpected error in Gemini stream: {str(e)} - Data: {buffer}")yield f"Error in stream: {str(e)}"buffer = ""breakexcept httpx.HTTPStatusError as e:response_text = await e.response.aread()102 unmodified linesasync for response_chunk, openai_messages, anthropic_messages, gemini_messages in query_model(query, providers, history):yield "", openai_messages, anthropic_messages, gemini_messages# Final yield to clear the query textboxyield "", openai_messages, anthropic_messages, gem | ini_messages# Gradio interfacedef clear_history():
app.py+51-31