Spaces:
Sleeping
Sleeping
logging
Browse files
app.py
CHANGED
|
@@ -1,12 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import asyncio
|
|
|
|
| 3 |
from utils.generator import generate, generate_streaming
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# ---------------------------------------------------------------------
|
| 6 |
# Wrapper function to handle async streaming for Gradio
|
| 7 |
# ---------------------------------------------------------------------
|
| 8 |
def generate_streaming_wrapper(query: str, context: str):
|
| 9 |
"""Wrapper to convert async generator to sync generator for Gradio"""
|
|
|
|
|
|
|
| 10 |
async def _async_generator():
|
| 11 |
async for chunk in generate_streaming(query, context):
|
| 12 |
yield chunk
|
|
@@ -14,27 +28,33 @@ def generate_streaming_wrapper(query: str, context: str):
|
|
| 14 |
# Create a new event loop for this thread
|
| 15 |
try:
|
| 16 |
loop = asyncio.get_event_loop()
|
|
|
|
| 17 |
except RuntimeError:
|
| 18 |
loop = asyncio.new_event_loop()
|
| 19 |
asyncio.set_event_loop(loop)
|
|
|
|
| 20 |
|
| 21 |
# Convert async generator to sync generator
|
| 22 |
async_gen = _async_generator()
|
| 23 |
|
| 24 |
# Accumulate chunks for Gradio streaming
|
| 25 |
accumulated_text = ""
|
|
|
|
| 26 |
|
| 27 |
while True:
|
| 28 |
try:
|
| 29 |
chunk = loop.run_until_complete(async_gen.__anext__())
|
| 30 |
accumulated_text += chunk
|
|
|
|
| 31 |
yield accumulated_text # Yield the accumulated text, not just the chunk
|
| 32 |
except StopAsyncIteration:
|
|
|
|
| 33 |
break
|
| 34 |
|
| 35 |
# ---------------------------------------------------------------------
|
| 36 |
# Gradio Interface with MCP support and streaming
|
| 37 |
# ---------------------------------------------------------------------
|
|
|
|
| 38 |
ui = gr.Interface(
|
| 39 |
fn=generate_streaming_wrapper, # Use streaming wrapper function
|
| 40 |
inputs=[
|
|
@@ -63,6 +83,8 @@ ui = gr.Interface(
|
|
| 63 |
|
| 64 |
# Launch with MCP server enabled
|
| 65 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 66 |
ui.launch(
|
| 67 |
server_name="0.0.0.0",
|
| 68 |
server_port=7860,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import asyncio
|
| 3 |
+
import logging
|
| 4 |
from utils.generator import generate, generate_streaming
|
| 5 |
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(
|
| 8 |
+
level=logging.INFO,
|
| 9 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
| 10 |
+
handlers=[
|
| 11 |
+
logging.StreamHandler(),
|
| 12 |
+
logging.FileHandler('app.log')
|
| 13 |
+
]
|
| 14 |
+
)
|
| 15 |
+
logger = logging.getLogger(__name__)
|
| 16 |
+
|
| 17 |
# ---------------------------------------------------------------------
|
| 18 |
# Wrapper function to handle async streaming for Gradio
|
| 19 |
# ---------------------------------------------------------------------
|
| 20 |
def generate_streaming_wrapper(query: str, context: str):
|
| 21 |
"""Wrapper to convert async generator to sync generator for Gradio"""
|
| 22 |
+
logger.info(f"Starting generation request - Query length: {len(query)}, Context length: {len(context)}")
|
| 23 |
+
|
| 24 |
async def _async_generator():
|
| 25 |
async for chunk in generate_streaming(query, context):
|
| 26 |
yield chunk
|
|
|
|
| 28 |
# Create a new event loop for this thread
|
| 29 |
try:
|
| 30 |
loop = asyncio.get_event_loop()
|
| 31 |
+
logger.debug("Using existing event loop")
|
| 32 |
except RuntimeError:
|
| 33 |
loop = asyncio.new_event_loop()
|
| 34 |
asyncio.set_event_loop(loop)
|
| 35 |
+
logger.debug("Created new event loop")
|
| 36 |
|
| 37 |
# Convert async generator to sync generator
|
| 38 |
async_gen = _async_generator()
|
| 39 |
|
| 40 |
# Accumulate chunks for Gradio streaming
|
| 41 |
accumulated_text = ""
|
| 42 |
+
chunk_count = 0
|
| 43 |
|
| 44 |
while True:
|
| 45 |
try:
|
| 46 |
chunk = loop.run_until_complete(async_gen.__anext__())
|
| 47 |
accumulated_text += chunk
|
| 48 |
+
chunk_count += 1
|
| 49 |
yield accumulated_text # Yield the accumulated text, not just the chunk
|
| 50 |
except StopAsyncIteration:
|
| 51 |
+
logger.info(f"Generation completed - Total chunks: {chunk_count}, Final text length: {len(accumulated_text)}")
|
| 52 |
break
|
| 53 |
|
| 54 |
# ---------------------------------------------------------------------
|
| 55 |
# Gradio Interface with MCP support and streaming
|
| 56 |
# ---------------------------------------------------------------------
|
| 57 |
+
logger.info("Initializing Gradio interface")
|
| 58 |
ui = gr.Interface(
|
| 59 |
fn=generate_streaming_wrapper, # Use streaming wrapper function
|
| 60 |
inputs=[
|
|
|
|
| 83 |
|
| 84 |
# Launch with MCP server enabled
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
logger.info("Starting ChatFed Generation Module server")
|
| 87 |
+
logger.info("Server will be available at http://0.0.0.0:7860")
|
| 88 |
ui.launch(
|
| 89 |
server_name="0.0.0.0",
|
| 90 |
server_port=7860,
|