aidevhund commited on
Commit
31d485f
·
verified ·
1 Parent(s): 5b71f43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -35
app.py CHANGED
@@ -7,63 +7,87 @@ from llama_parse import LlamaParse
7
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
8
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
9
 
10
- # Define the LLM model and document processing
11
  llm = HuggingFaceInferenceAPI(model_name="tiiuae/falcon-7b-instruct")
12
  parser = LlamaParse(api_key='llx-zKtsC5UBLs8DOApOsLluXMBdQhC75ea0Vs80SmPSjsmDzuhh', result_type='markdown')
 
 
13
  file_extractor = {'.pdf': parser}
14
  documents = SimpleDirectoryReader('data/', file_extractor=file_extractor).load_data()
15
 
 
16
  embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
17
  vector_index = VectorStoreIndex.from_documents(documents, embed_model=embed_model)
18
  query_engine = vector_index.as_query_engine(llm=llm)
19
 
20
- # Function to process query with retries
 
 
 
 
 
 
 
 
21
  def query_with_retry(query, max_retries=3, wait_time=5):
22
  for attempt in range(max_retries):
23
  try:
24
  start_time = datetime.now()
25
- response = query_engine.query(query)
26
  end_time = datetime.now()
27
  duration = (end_time - start_time).total_seconds()
 
28
  return response
29
  except httpx.ReadTimeout:
30
  if attempt < max_retries - 1:
 
31
  time.sleep(wait_time)
32
  else:
33
  raise
34
  except Exception as e:
35
- return f"An error occurred: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- # Chatbot interface function
38
- def chatbot_interface(user_input, history=None):
39
- if history is None:
40
- history = [] # Initialize history if it is None
41
-
42
- if not user_input:
43
- return history, history
44
-
45
- bot_response = query_with_retry(user_input)
46
-
47
- # Append messages to chat history
48
- history.append(("User", user_input))
49
- history.append(("Bot", bot_response.strip()))
50
-
51
- return history, history
 
 
 
 
 
 
 
 
 
52
 
53
- # Define the Gradio interface
54
- iface = gr.Interface(
55
- fn=chatbot_interface,
56
- inputs=[
57
- gr.Textbox(label="Ask a question", placeholder="Enter your query here...", lines=2),
58
- gr.State() # Store chat history
59
- ],
60
- outputs=[
61
- gr.Chatbot(label="Chat"),
62
- gr.State() # Output the updated chat history
63
- ],
64
- title="Document-based Question Answering Chatbot",
65
- description="Ask questions based on the documents you uploaded. This model answers queries using your uploaded PDFs.",
66
- live=False # live=True can cause issues with chat interfaces
67
- )
68
 
69
- iface.launch()
 
7
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
8
  from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
9
 
10
+ # LLM ve Parser Başlatma
11
  llm = HuggingFaceInferenceAPI(model_name="tiiuae/falcon-7b-instruct")
12
  parser = LlamaParse(api_key='llx-zKtsC5UBLs8DOApOsLluXMBdQhC75ea0Vs80SmPSjsmDzuhh', result_type='markdown')
13
+
14
+ # PDF dosyasını yükleyip indexleme işlemi
15
  file_extractor = {'.pdf': parser}
16
  documents = SimpleDirectoryReader('data/', file_extractor=file_extractor).load_data()
17
 
18
+ # Embedding Modeli ve Query Engine Başlatma
19
  embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
20
  vector_index = VectorStoreIndex.from_documents(documents, embed_model=embed_model)
21
  query_engine = vector_index.as_query_engine(llm=llm)
22
 
23
+ # System Prompt: LLM'nin görevini belirlemek için
24
+ system_prompt = """
25
+ You are an AI assistant designed to answer questions about the Hund Ecosystem based on the uploaded PDF document.
26
+ Your primary responsibility is to provide detailed, accurate, and clear answers to user queries related to the content of the document.
27
+ For any question that is not related to the content of the document, kindly ask the user to refer to the Hund Ecosystem.
28
+ Please ensure to be polite and professional in your responses. If the question cannot be answered based on the document, kindly guide the user accordingly.
29
+ """
30
+
31
+ # Sorgu işlemi için retry mekanizması
32
  def query_with_retry(query, max_retries=3, wait_time=5):
33
  for attempt in range(max_retries):
34
  try:
35
  start_time = datetime.now()
36
+ response = query_engine.query(query, system_message=system_prompt) # System prompt ekleniyor
37
  end_time = datetime.now()
38
  duration = (end_time - start_time).total_seconds()
39
+ print(f"Query completed in {duration:.2f} seconds.\n {response}")
40
  return response
41
  except httpx.ReadTimeout:
42
  if attempt < max_retries - 1:
43
+ print(f"Timeout occurred. Retrying in {wait_time} seconds...")
44
  time.sleep(wait_time)
45
  else:
46
  raise
47
  except Exception as e:
48
+ print(f"An error occurred: {e}")
49
+ break
50
+
51
+ # Arayüz fonksiyonları
52
+ def chatbot_interface(input_text, history):
53
+ if input_text:
54
+ response = query_with_retry(input_text)
55
+ history.append(("Hundler", input_text))
56
+ history.append(("HundAI Chatbot", response))
57
+ return history, "" # Chat geçmişini döndür
58
+
59
+ def clear_all():
60
+ return [], "" # Sohbet geçmişini ve metni temizler
61
 
62
+ # Gradio Arayüzü
63
+ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]), css="""
64
+ footer {visibility: hidden;}
65
+ #input-box {background-color: #2e2e2e; color: #e5e5e5;}
66
+ #submit-btn {background-color: #607D8B; color: #fff; border-radius: 8px; padding: 10px 20px; font-weight: bold;}
67
+ #retry-btn {background-color: #8e8e8e; color: #fff; border-radius: 8px; padding: 10px 20px; font-weight: bold;}
68
+ #clear-btn {background-color: #f44336; color: #fff; border-radius: 8px; padding: 10px 20px; font-weight: bold;}
69
+ #undo-btn {background-color: #2196F3; color: #fff; border-radius: 8px; padding: 10px 20px; font-weight: bold;}
70
+ #output-box {background-color: #2e2e2e; color: #fff;}
71
+ #submit-btn:hover, #retry-btn:hover, #clear-btn:hover, #undo-btn:hover {cursor: pointer; opacity: 0.9;}
72
+ #buttons {display: flex; justify-content: space-between; font-size: 0.85em;}
73
+ """) as demo:
74
+ # Chatbot UI
75
+ with gr.Column():
76
+ chatbox = gr.Chatbot(label="Chatbot", elem_id="chatbox")
77
+ input_text = gr.Textbox(label="Ask a question", placeholder="Type your question here...", lines=2, elem_id="input-box")
78
+
79
+ # Butonlar: Retry, Clear, Undo butonları küçük şekilde düzenlendi
80
+ with gr.Row(elem_id="buttons"):
81
+ retry_btn = gr.Button("Retry", elem_id="retry-btn", scale=0.8)
82
+ clear_btn = gr.Button("Clear", elem_id="clear-btn", scale=0.8)
83
+ undo_btn = gr.Button("Undo", elem_id="undo-btn", scale=0.8)
84
+
85
+ submit_btn = gr.Button("Ask", elem_id="submit-btn")
86
 
87
+ # Button actions
88
+ submit_btn.click(chatbot_interface, inputs=[input_text, chatbox], outputs=[chatbox, input_text])
89
+ retry_btn.click(chatbot_interface, inputs=[input_text, chatbox], outputs=[chatbox, input_text]) # Retry işlemi
90
+ clear_btn.click(clear_all, outputs=[chatbox, input_text])
91
+ undo_btn.click(clear_all, outputs=[chatbox, input_text]) # Undo işlevi Clear olarak kullanıldı
 
 
 
 
 
 
 
 
 
 
92
 
93
+ demo.launch()