File size: 2,824 Bytes
91e999e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9beccbd
 
 
91e999e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import os
import gradio as gr
import asyncio
import sys
import logging
from fastapi import FastAPI
import uvicorn
from threading import Thread

# Add the current directory to the path so we can import from app
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# Import your main application
from main import app as fastapi_app, startup_event, generate_content, ContentRequest

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Initialize the FastAPI app in a separate thread
def start_fastapi():
    # Don't run startup event here, as we'll run it manually
    # This prevents duplicate initialization
    uvicorn.run(fastapi_app, host="0.0.0.0", port=8080, lifespan="off")

# Start FastAPI in a separate thread
thread = Thread(target=start_fastapi, daemon=True)
thread.start()

# Run the startup event to initialize the model and index
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(startup_event())

# Define the Gradio interface
def generate(grade, subject, topic, style):
    try:
        # Create a ContentRequest object
        request = ContentRequest(
            grade=int(grade),
            subject=subject,
            topic=topic,
            style=style
        )
        
        # Call the generate_content function
        response = loop.run_until_complete(generate_content(request))
        
        return response["response"]
    except Exception as e:
        logger.error(f"Error generating content: {str(e)}")
        return f"Error: {str(e)}"

# Create the Gradio interface
with gr.Blocks(title="Swahili Content Generation") as demo:
    gr.Markdown("# Swahili Content Generation")
    gr.Markdown("Generate educational content in Swahili for primary school students.")
    
    with gr.Row():
        with gr.Column():
            grade = gr.Dropdown(
                choices=["3", "4"], 
                label="Grade Level",
                value="3"
            )
            subject = gr.Dropdown(
                choices=["math", "science"], 
                label="Subject",
                value="math"
            )
            topic = gr.Textbox(label="Topic")
            style = gr.Dropdown(
                choices=["normal", "simple", "creative"], 
                label="Style",
                value="normal"
            )
            generate_btn = gr.Button("Generate Content")
        
        with gr.Column():
            output = gr.Textbox(label="Generated Content", lines=15)
    
    generate_btn.click(
        fn=generate,
        inputs=[grade, subject, topic, style],
        outputs=output
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)