Team Ai
Apppublic

melghorab/code-assistant

sourceHugging Faceopenrailupdated 2y agoView on Hugging Face
0likes
app.cpython-310.pyc48 linesDownload Raw Back to __pycache__
1o

2���g�1�@s�ddlZddlZddlmZddlmZmZddlZddlZddl	m3Z4ddlmZm
Z
mZddlmZmZddlmZddlmZdd	lmZdd5lmZmZddlmZddlmZmZdd
lmZddlZddlm Z m!Z!m"Z"ddl#m$Z$ddl%m&Z&ddl'mZddl(m)Z)ddl*m+Z+m,Z,ddl-Z.ddl/Z/ddl0Z0ddl1Tddl2m3Z3m4Z4ddl5m6Z6m7Z7m8Z8ddl9m:Z:dej;d<da<da=da>da?da(e.j@dd��Z@e.jAde.jBfdd��ZAe.jCdd��ZCdS) �N)�6itemgetter)�List�Dict)�7ChatOpenAI)�START�8StateGraph�END)r�	TypedDict��Document)�ChatPromptTemplate)�StrOutputParser)�Tool�tool)�ToolNode)r	�	Annotated)�add_messages)�BaseMessage�HumanMessage�	AIMessage)�Qdrant)�OpenAIEmbeddings)�QdrantClient)�Distance�VectorParams)�*)�search_pypi�
write_to_docx)�describe_imports�main_prompt�documenter_prompt)�9AgentStatez�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�OPENAI_API_KEYc�s�tjdd���IdHdS)Nz_Welcome to the Python Code Documentation Assistant! Please upload a Python file to get started.��content)�cl�Message�send�r(r(�D/home/melghorab/maven_nlp_course/code/Project/Code_Documenter/app.py�
on_chat_start5s�r*�messagec+10�s�|j�r1tdd�|jD���r1dd�|jD�}|d}|j�d�p,|j�d�p,|jdk}|�r$tjd	d11�}|��IdHt	d|�d��t12��}tj
�||j�}t|j
d
��&}|��}t|d��
}	|	�|�Wd�n1sqwYWd�n1s�wY|a�zxt|�}13t|14|�}t	d�tg}tdd�}
t�dtfdg�}td�td�d�|B|
Bt�B�t	d��fdd�}t|�}tt�}|�d|�|�d|�|� d�dd�}|�!d|�|�"dd�|�#�}t	d�dd t$�%d!|d��d"�gi}d#|_&|�'�IdHtjd#d15�}|��IdH|�(|�}t)d$�a*t+d%d�}d&}t*j,d't-|t.j/d(�d)�t0t*d'|d*�a1|dd+j&}d,d,|d-�g}d.d�|D�}t1�2|�t1j3d/d0id1�}t	d2�tdd�}t�dt4fg�}d3td3�i|B|Bt�B}t	d4�d'}t*j5|d5d6�d} | dj6}!d7|!d8d9�d:|!d8d;�d<|!d8d=��}"t	d>�|�(d3|"i�}#t	d?�t7|#�a8t	d@�tdddA�}$t�dt9fdBg�}%tdC�|Btd�tdC�dD�|%B|$Bt�Ba:t	dE�tj;dFt8dGdH�g}&t	dI�dJ|_&|&|_|�'�IdHWdSt<�y#}'zdKt=|'���|_&|�'�IdHWYd}'~'dSd}'~'wwtjdLd16���IdHdSt�r�t:�r�|j&}(tjdMd17�}|��IdHzdN|(dO�})t:�(|)�}*|*|_&|�'�IdHWdSt<�y�}'zdPt=|'���|_&|�'�IdHWYd}'~'dSd}'~'wwtjdQd18���IdHdS)RNcss�|]}|jdkVqdS)�fileN��type��.0�elr(r(r)�	<genexpr>=s�zon_message.<locals>.<genexpr>cSsg|]	}|jdkr|�qS)r,r-r/r(r(r)�19<listcomp>>szon_message.<locals>.<listcomp>rz
text/x-pythonz.pyz20text/plainzProcessing your Python file...r#zfile element 21 z 22�rb�wbzDone reading filezgpt-4o-mini)�model�system)�humanz	{imports}�
code_language�imports�r9r:zdone defining imports chaincsD|dd}t�|j�}|d|dd�}��|�}dt|d�giS)N�messages�����r9r:r;r#)�json�loadsr$�invoker)�state�last_messager$Zchain_input�response�Zdescribe_imports_chainr(r)�call_imports_chainrs�23z&on_message.<locals>.call_imports_chainZ
imports_agent�imports_actioncSs|dd}|jrdStS)Nr<r=rF)�24tool_callsr)rArBr(r(r)�should_continue�sz#on_message.<locals>.should_continuezcompiled imports graphr<r8�python)�roler$z3Analyzing your code and generating documentation...z:memory:ztext-embedding-3-smalliZdescription_rag_data)�size�distance)�collection_nameZvectors_config)rMZ25embeddingsr=zImported Packages)r.�namer$c	Ss8g|]}t|d�d|d�d|d��i|�d��qS)r.z - rNr$)Zpage_content�metadatar26)r0�chunkr(r(r)r3�s����k�)Z
search_kwargszdone adding docs to DB�contextzdone defining documenter chaini�)rM�limitztype: rOr.z 27name: rNz 28content: r$zdone extracting chunks form DBz5done invoking documenter chain and will write in docxzdone writing docx file)r6�temperature)r8z{query}�query)rSr9rVzdone defining main chainzdocumentation.docx�inline)rN�path�displayzdone defining elementsuz✅ Your Python file has been processed! You can download the documentation file below. How can I help you with your code?u❌ Error processing file: z"Please upload a Python (.py) file.zThinking...�Python)r9rVu$❌ Error processing your question: z:Please upload a Python file first before asking questions.)>�elements�any�mime�29startswithrN�endswithr%r&r'�print�tempfile�mkdtemp�osrX�join�open�read�write�processed_file_pathZread_python_fileZextract_importsrrr�
from_messagesrrr
rrr!Zadd_nodeZset_entry_pointZadd_conditional_edgesZadd_edge�compiler>�dumpsr$�updater@r�
qdrant_clientrZcreate_collectionrrZCOSINEr�vectorstoreZ
add_documentsZas_retrieverr �scroll�payloadr�document_file_pathr�30main_chain�File�	Exception�str)+r+Z
file_elementsZfile_elementZis_python_file�msgZtemp_dir�	file_pathZsource_fileZfile_content_bytesZdestination_fileZfile_contentr:Zsearch_packages_toolsZdescribe_imports_llmZdescribe_imports_promptrEZimports_tool_nodeZuncompiled_imports_graphrHZcompiled_imports_graphZ
initial_state�resultZembedding_modelZ
embedding_dim�text�chunks�docsZqdrant_retrieverZdocumenter_llmZdocumenter_llm_promptZdocumenter_chainrMZ31all_pointsZ	one_chunkZ32input_textZdocument_responseZmain_llmZmain_llm_promptr[�eZ33user_input�inputsrCr(rDr)�34on_message9s6�35�����3637�����	38����3940�41��4243�44����45.���������	�46��	r~cCs6trtj�tj�t��rt�tj�t��dSdSdS)N)rhrcrX�exists�dirname�shutil�rmtreer(r(r(r)�on_stop8s�r�)Drc�getpass�operatorr�typingrrr>�requestsZlangchain_openairZlanggraph.graphrrr�typing_extensionsr	Zlangchain_core.documentsr�langchain_core.promptsrZlangchain.schema.output_parserr
Zlangchain_core.toolsrrZlanggraph.prebuiltrrZlanggraph.graph.messager�langchain_core.messagesrrrZlangchain.vectorstoresrZlangchain.embeddingsr�langchain.schemarmrZqdrant_client.http.modelsrr�chainlitr%rar�Z
code_analysis�toolsrr�promptsrrr Zstatesr!�environrhrqrnrrr*r~r&r�r(r(r(r)�<module>sV4748