o
    iT                     @   s  d Z ddlmZmZ ddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlZddlZddlZddlZddlZddlmZmZ ddlZddlmZmZ dd	lmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* dd
l+m,Z, ddl-m.Z. ej/ej0dd e1e2Z3eddddZ4dZ5ej6e5dd e4j7dee5ddd G dd deZ8G dd deZ9dd Z:dd  Z;e4<d!d"d# Z=e4<d$d%d& Z>e4?d'd(e8d)ed*efd+d,Z@e4?d-d)e9d*efd.d/ZdS )0a  
================================================================================
 Complete FastAPI Application for Storigo Content Generation - v7.0
 - Professional slide content generation with inline image fetching
 - Guaranteed image field presence (URL or null)
 - Comprehensive error handling and logging
 - Multiple endpoints support
================================================================================
    )FastAPIHTTPException)StaticFiles)	BaseModelField	validator)JSONResponseFileResponseN)UnionDict)RequestBody)load_pdf
transcribeload_txt split_text_with_semantic_chunker	crawlerrrread_file_url(split_text_with_semantic_chunker_for_urlclean_using_llmcount_total_wordssave_documents_to_txtcreate_and_save_embeddingsgenerate_slide_contentmerge_all_faissparsingmarks_splitterallocate_slidescreate_embeddingsgenerate_slide_content_alloc1)"generate_slide_content_from_prompt)OllamaEmbeddingsz7%(asctime)s - [%(levelname)s] - (FastAPI) - %(message)s)levelformatStorigo Content Generation APIzNProfessional API for generating presentation content with images using Groq AI7.0.0)titledescriptionversiongenerated_images_for_storigoT)exist_okz/images)	directoryimages)namec                   @   sx   e Zd ZU dZeed< eed< eed< eeef ed< eeef ed< eed< eed< eeef ed	< G d
d dZ	dS )StorigoContentRequestz1Request model for prompt-based content generation	client_idprompt
num_slidesis_imageis_questionnum_mcqsquestion_positionisGPUc                
   @   s&   e Zd Zdddddddddd	iZd
S )zStorigoContentRequest.Configexample   z;Generate a professional presentation about kitchen handling
         endT)r/   r0   r1   r2   r3   r4   r5   r6   N__name__
__module____qualname__json_schema_extra rB   rB   X/var/www/eduai.edurigo.com/image_generation/testing/image_generation_fast_api_testing.pyConfigG   s    rD   N)
r>   r?   r@   __doc__int__annotations__strr
   boolrD   rB   rB   rB   rC   r.   <   s   
 r.   c                   @   s   e Zd ZU dZeed< eed< eed< eeef ed< eed< eeef ed< eed< eed	< eeef ed
< G dd dZ	dS )DocumentContentRequestz3Request model for document-based content generationr/   r1   document_urlr2   document_typer3   r4   r5   r6   c                   @   s(   e Zd Zdddddddddd	d
	iZdS )zDocumentContentRequest.Configr7   
client_123r9   z https://example.com/document.pdf1pdfr;   r<   T)	r/   r1   rK   r2   rL   r3   r4   r5   r6   Nr=   rB   rB   rB   rC   rD   a   s    rD   N)
r>   r?   r@   rE   rH   rG   rF   r
   rI   rD   rB   rB   rB   rC   rJ   U   s   
 rJ   c              
   C   sb   zt j| dd}|jdkr|jW S tdd|  d ty0 } ztddt| dd	}~ww )
zFetch document from URL   )timeout   i  zDocument not found at status_codedetail  zError fetching document: N)requestsgetrT   contentr   	ExceptionrH   )urlresponseerB   rB   rC   fetch_document_with_curlt   s   
r^   c              
   C   s   z't | | t|}t||||||||}	td| d |	 }
t|
dW S  tyS } z tdt	|  tdt	| t
 dddW  Y d	}~S d	}~ww )
z'Process documents and generate responsezSlide content generated for z slides.rY   zError generating content: Error: errordetailsrV   rY   rT   N)r   r   r   loggerinfodictr   rZ   rb   rH   	traceback
format_exc)split_documentsr/   r1   r4   r2   r3   r5   r6   merge_embeddingsslide_contentresponse_datar]   rB   rB   rC   process_and_generate_response   s$   
rn   /c                      s    ddddddddg d	d
S )zAPI information endpointr$   r%   operational%/generate-storigo-content-from-prompt/generate-storigo-content/healthz/images/{filename})generate_from_promptgenerate_from_documenthealthr,   )z3Professional slide content generation using Groq AIz4Inline synchronous image generation (no null values)z,MCQ generation with customizable positioningz,Document-based content generation (PDF/HTML)z(Comprehensive error handling and loggingzStatic image serving)r-   r(   status	endpointsfeaturesrB   rB   rB   rB   rC   root   s   rz   rs   c                      s   ddt   ddS )zHealth check endpointhealthyr%   z)Storigo Content Generation API is running)rw   r(   	timestampmessage)timerB   rB   rB   rC   health_check   s   r   rq   payloadrequestreturnc              
      sZ  zjt d t d t d t d t d| j  t d| jdd  d t d	| j  t d
| j  t d| j  t d| j  t d| j  t d t	| jt
rl| jdk}nt	| jtrz| j dv }nt| j}t d|  dtdtfdd}||d}t d|  t }t| j| j| j|t| j| jt	| jtrt
| jn| jd}t | }t d|dd t d t d t d i t|ddd}|j D ]k\}	}
t|
dr|
j|
j|
j|
jd|d  |	< qt|
d!d}|dur@t| }|r#| d"kr&d}n|d#r1|| }n|d$s=|d%r>nd}|
jt|
d&dt|
d'dt|
d(d|d)|d  |	< qt d* td+d, |d   D }td-d, |d   D }d.}d.}|d   D ]@\}}|d/rd!|vrt d0| d1 q|d! r|d7 }t d2| d3|d!   q|d7 }t d4| d5 qt d t d6 t d7|dd t d8t |d    t d	|  t d
|  |r|d.krt d9|  t d:|  t d;|| d< d=d> t d?|d   t d g }|d   D ]\}}|d/rEd!|vrE|!| q1|r\t d@t | dA t"dB| t dC t dD t#|dEdFW S  t$y } z3t d t dG t dHt|  t d t t%&  t#t|t%& dIdJdFW  Y d}~S d}~ww )Kz
    Generate professional Storigo content from a text prompt with inline image generation.

    Returns JSONResponse where every slide has an 'image' field (absolute URL or null).
    P================================================================================u8   🚀 API ENDPOINT: /generate-storigo-content-from-prompt   📋 Request Parameters:   - Client ID: z   - Prompt: NP   z...   - Slides:    - MCQs: z   - Images: z   - Questions: z   - Question Position: r:   )rN   trueTrueyesu   🎨 Image generation enabled: reqr   c                 S   s   | j d}| j d}|r)|r)|dd  }|dd  }| d| S | j d}|r:| jj d| S t| jdS )Nzx-forwarded-protozx-forwarded-host,r   z://hostro   )	headersrX   splitstripr[   schemerH   base_urlrstrip)r   forwarded_protoforwarded_hostprotor   rB   rB   rC   _compute_base_url   s   zHgenerate_storigo_content_from_prompt_endpoint.<locals>._compute_base_urlro   u#   🌐 Computed base URL for images: )r0   r1   r4   r2   r3   r5   GPUu   ⏱️  Total generation time: z.2fsu   📦 PREPARING API RESPONSEtoken_count)slidesr   question)typer   optionscorrect_answerr   imagenonez/images/zhttp://zhttps://
subheading
paragraphsvisualization_suggestion)r   r   r   r   r   u   🔍 Response Validation:c                 s       | ]
}| d rdV  qdS )slide_r:   N
startswith.0krB   rB   rC   	<genexpr>:      z@generate_storigo_content_from_prompt_endpoint.<locals>.<genexpr>c                 s   r   )mcq_r:   Nr   r   rB   rB   rC   r   ;  r   r   r   u   ❌ CRITICAL: z missing 'image' field!u     ✅ z
: image = u	     ⚠️ z: image = nullu   📊 RESPONSE SUMMARY:z   - Generation time: z   - Total items: z   - Images assigned: z   - Images null: z   - Success rate: d   z.1f%z   - Token count: u   ❌ CRITICAL ERROR: z slides missing 'image' fieldz,Internal error: slides missing image field: u4   ✅ Response validation passed - returning to clientQ================================================================================
rR   rd   u   ❌ API ENDPOINT ERRORr`   ra   rV   )'re   rf   r/   r0   r1   r4   r2   r3   r5   
isinstancerF   rH   r   rI   r   r   r~   r    r6   getattrr   itemshasattrr   r   r   r   lowerr   sumkeysrb   lenappend
ValueErrorr   rZ   rh   ri   )r   r   is_image_boolr   r   generation_startstorigo_contentgeneration_timerm   	slide_key	slide_objimage_valuetotal_slides
total_mcqsimages_assignedimages_nullkeydatamissing_image_fieldr]   rB   rB   rC   -generate_storigo_content_from_prompt_endpoint   s   

















	










r   rr   c              
      s  zt d t d t d t d t d| j  t d| j  t d| j  t d| j  t d| j  t d | jd	kr>t d
 t d| j  t| j}t dt| d t	j
ddd}|| |j}t d|  W d   n1 sw   Y  zt|}tdddd}|| W d   n1 sw   Y  ddg}tdddd}| }W d   n1 sw   Y  t||}t dt| d t|| jdd}	t|| j}
t||	| j| j| j| j| j| j}| }t d t d t|d W tj|r)t| t d! W S W S tj|r=t| t d! w w | jd"krt d# t d$| j  d%| j v r|t d& t| j}t d'd(}
t!|}t"||
}t#|}t$||
}n5t d) t%| jI dH }t d*|  t&|}|st'd+d,d-t#|}t d. t d'd(}
t$||
}t d/ t(|| j| j| j| j| j| j| jW S t'd0d1d- t'y     t)y
 } z't *d2t+|  t *t,-  td3t+| t,- d4d+d5W  Y d}~S d}~ww )6a  
    Generate Storigo content from document URL (PDF or HTML)
    
    This endpoint processes documents and generates presentation content:
    - PDF documents with text extraction
    - HTML content from web pages
    - YouTube video transcriptions
    - Semantic chunking and embeddings
    
    Args:
        request: DocumentContentRequest with document URL and configuration
        
    Returns:
        JSONResponse with slides and optional MCQs
    r   u,   🚀 API ENDPOINT: /generate-storigo-contentr   r   z   - Document URL: z   - Document Type: r   r   rO   u   📄 Processing PDF document...z   Fetching from: u      ✅ Document fetched: z bytesFz.pdf)deletesuffixu      📁 Temporary file: Nzparse_data.mdwzutf-8)encoding)#zHeader 1)z##zHeader 2ru      ✅ Split into z chunksr   )	min_charsu   ✅ Content generation completer   r_   u%      🗑️  Cleaned up temporary filehtmlu   🌐 Processing HTML content...z   URL: youtubeu0      📹 YouTube video detected - transcribing...znomic-embed-text)modelu       🕷️  Crawling web page...u      ✅ Content saved to: rV   zFailed to read content from URLrS   u#      ✅ Extracted meaningful contentu6      🔄 Processing documents and generating content...i  z/Unsupported document_type. Use 'pdf' or 'html'.u"   ❌ Error in document processing: r`   ra   rd   ).re   rf   r/   rK   rL   r1   r4   r^   r   tempfileNamedTemporaryFilewriter-   r   openreadr   r   r   r   r2   r3   r5   r6   rg   r   ospathexistsremover   r   r!   r   r   r   r   r   r   r   rn   rZ   rb   rH   rh   ri   )r   document_content	temp_filetemp_file_path
parse_datafheaders_to_split_onrY   chunks
allocation
embeddingsrl   rm   	file_pathtextsplit_documents1meaningful_contentrj   filenameraw_contentr]   rB   rB   rC   generate_storigo_contentw  s   

























r   )ArE   fastapir   r   fastapi.staticfilesr   pydanticr   r   r   fastapi.responsesr   r	   r   rW   r   loggingrh   typingr
   r   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   $generate_storigo_content_from_promptr    langchain_community.embeddingsr!   basicConfigINFO	getLoggerr>   re   appIMAGES_DIR_NAMEmakedirsmountr.   rJ   r^   rn   rX   rz   r   postr   rB   rB   rB   rC   <module>   s`    
P


	 :