AhmedEwis commited on
Commit
757b4e5
1 Parent(s): f18061a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -9
app.py CHANGED
@@ -615,10 +615,11 @@ With every Rowad program, participants will be mentored by seasoned bankers and
615
 
616
 
617
 
618
-
619
  import gradio as gr
620
  import os
621
  import csv
 
 
622
 
623
  #State of Union Text
624
 
@@ -634,6 +635,16 @@ from langchain.vectorstores.faiss import FAISS
634
  from langchain.text_splitter import RecursiveCharacterTextSplitter
635
  from langchain import OpenAI, VectorDBQA
636
 
 
 
 
 
 
 
 
 
 
 
637
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
638
  texts = text_splitter.split_text(state_of_the_union)
639
 
@@ -648,21 +659,37 @@ chain = load_chain("lc://chains/vector-db-qa/stuff/chain.json", vectorstore=vect
648
  def run_chain(query):
649
  return chain.run(query)
650
 
651
- def save_question_to_csv(file_path, question):
652
- with open(file_path, 'a', newline='', encoding='utf-8') as csvfile:
653
- writer = csv.writer(csvfile)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
654
  writer.writerow([question])
655
 
 
 
 
 
 
 
656
  inputs = gr.inputs.Textbox(lines=1, label="Enter your Question:")
657
  outputs = gr.outputs.Textbox(label="Answer:")
658
 
659
-
660
- # Provide an absolute path to the CSV file
661
- csv_file_path = os.path.abspath('questions.csv')
662
-
663
  def run_app(input):
664
  answer = run_chain(input)
665
- save_question_to_csv(csv_file_path, input)
666
  return answer
667
 
668
  gr.Interface(fn=run_app, inputs=inputs, outputs=outputs, title="The following is a conversation with a human called Shegardi. Shegardi is helpful, precise, truthful, and very friendly. Also, Shegardi is an employee of Warba Bank, located in Kuwait. Shegardi will only use the information provided to him.").launch()
 
615
 
616
 
617
 
 
618
  import gradio as gr
619
  import os
620
  import csv
621
+ from io import StringIO
622
+ from huggingface_hub import hf_hub_download, HfApi, HfFolder
623
 
624
  #State of Union Text
625
 
 
635
  from langchain.text_splitter import RecursiveCharacterTextSplitter
636
  from langchain import OpenAI, VectorDBQA
637
 
638
+ # Authenticate with your Hugging Face API key
639
+ hf_api = HfApi()
640
+ hf_folder = HfFolder()
641
+ hf_api.login(hf_folder.get_token('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'))
642
+
643
+ # Hugging Face space variables
644
+ namespace = "AhmedEwis"
645
+ repo_name = "Shagardi_chatbot"
646
+ csv_file_path = "questions.csv"
647
+
648
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
649
  texts = text_splitter.split_text(state_of_the_union)
650
 
 
659
  def run_chain(query):
660
  return chain.run(query)
661
 
662
+ def download_csv_from_space():
663
+ file_url = hf_hub_download(repo_id=f"{namespace}/{repo_name}", filename=csv_file_path)
664
+ with open(file_url, "r") as f:
665
+ return f.read()
666
+
667
+ def save_question_to_csv(question):
668
+ csv_data = download_csv_from_space()
669
+ csvfile = StringIO(csv_data)
670
+ csvfile.seek(0)
671
+
672
+ reader = csv.reader(csvfile)
673
+ rows = list(reader)
674
+
675
+ with open(csv_file_path, 'w', newline='', encoding='utf-8') as f:
676
+ writer = csv.writer(f)
677
+ for row in rows:
678
+ writer.writerow(row)
679
  writer.writerow([question])
680
 
681
+ hf_api.upload_file(
682
+ path_or_fileobj=csv_file_path,
683
+ repo_id=f"{namespace}/{repo_name}",
684
+ file_name=csv_file_path,
685
+ )
686
+
687
  inputs = gr.inputs.Textbox(lines=1, label="Enter your Question:")
688
  outputs = gr.outputs.Textbox(label="Answer:")
689
 
 
 
 
 
690
  def run_app(input):
691
  answer = run_chain(input)
692
+ save_question_to_csv(input)
693
  return answer
694
 
695
  gr.Interface(fn=run_app, inputs=inputs, outputs=outputs, title="The following is a conversation with a human called Shegardi. Shegardi is helpful, precise, truthful, and very friendly. Also, Shegardi is an employee of Warba Bank, located in Kuwait. Shegardi will only use the information provided to him.").launch()