Spaces:
Runtime error
Runtime error
main code
Browse filesFiles changed (3)
- .gitattributes+1-1
- .gitignoreAdded+3
- app.py+70-4
31 unmodified lines3233343531 unmodified lines*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text31 unmodified lines3233343531 unmodified lines*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text
123flagged/**/**.env__pycache__/**/*1234567import gradio as grdef greet(name):return "Hello " + name + "!!"iface = gr.Interface(fn=greet, inputs="text", outputs="text")iface.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import gradio as grfrom PyPDF2 import PdfReader, PdfFileReaderfrom langchain.llms import OpenAIfrom langchain.schema import Documentfrom langchain.chains import AnalyzeDocumentChainfrom langchain.chains.question_answering import load_qa_chainfrom langchain.chat_models import ChatOpenAIfrom langchain.callbacks import get_openai_callbackimport osimport dotenvdotenv.load_dotenv('.env')APP_USER = os.getenv('APP_USER')APP_PASSWORD = os.environ.get('APP_PASSWORD')query_template_default= "As you're conducting research in the field of AI, consider the following research question '{question}'. If the answer isn't explicitly found in your current literature review, aim to extrapolate it indirectly. Should the required information be unattainable, please note it as 'Na'. Please remember to cite the sources that influenced your interpretations."def check_auth(username, password):return username == APP_USER and password == APP_PASSWORDdef read_pdf(file):try:# Use PyPDF2's PdfReader to extract textpdf_file_obj = open(file.name, 'rb')pdf_reader = PdfReader(pdf_file_obj)content = ''for page in pdf_reader.pages:page_text = page.extract_text()if page_text:content += page_textif not content:return "No text found in the PDF."return(file.name,content)except Exception as e:return f"Error reading PDF: {e}"def query(extracted_text, gpt_model, question,query_template):# Process text based on chosen operationif gpt_model == "gpt-3.5-turbo":llm = ChatOpenAI(temperature=0.7,model="gpt-3.5-turbo")elif gpt_model == "gpt-3.5-turbo-16k":llm = ChatOpenAI(temperature=0.7,model="gpt-3.5-turbo-16k")elif gpt_model == "gpt-4":llm = ChatOpenAI(temperature=0.7,model="gpt-4")else:llm= ChatOpenAI(temperature=0.7,model="gpt-3.5-turbo")qa_chain = load_qa_chain(llm, chain_type="map_reduce")qa_document_chain = AnalyzeDocumentChain(combine_docs_chain=qa_chain)query = query_template.format(question=question)#doc = [Document(page_content=extracted_text)]with get_openai_callback() as cb:answer = qa_document_chain.run(input_document=extracted_text, question=query)# Return processed text and feedbackreturn [f"Question:\n{question}\nAnser:\n{answer}",f"Total Tokens: {cb.total_tokens}\nPrompt Tokens: {cb.prompt_tokens}\nCompletion Tokens: {cb.completion_tokens}\nTotal Cost (USD): ${cb.total_cost}"]with gr.Blocks() as ui:file_output = gr.File()upload_button = gr.UploadButton( label="Upload PDF")extracted_text = gr.Text(label="Extracted Text")upload_button.upload(read_pdf, upload_button, [file_output,extracted_text])query_template = gr.Textbox(label="query_template",value=query_template_default)question = gr.Textbox(label="question")gpt_model = gr.Radio(choices=["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4"], label="Operation")result = gr.Text(label="result")cost = gr.Text(label="cost")query_btn = gr.Button("query")query_btn.click(query, inputs=[extracted_text,gpt_model,question,query_template], outputs=[result,cost])ui.launch(auth=check_auth)
.gitattributes+1-1
.gitignoreAdded+3
app.py+70-4