gufett0 commited on
Commit
8238f47
·
1 Parent(s): 5c3431a

added text streamer

Browse files
Files changed (1) hide show
  1. backend.py +3 -26
backend.py CHANGED
@@ -13,9 +13,6 @@ from llama_cpp import Llama
13
  import spaces
14
  from huggingface_hub import login
15
 
16
- from transformers import TextIteratorStreamer
17
- import threading
18
-
19
 
20
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
21
  login(huggingface_token)
@@ -72,29 +69,9 @@ def handle_query(query_str, chathistory):
72
  ("user", qa_prompt_str),
73
  ]
74
  text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
75
-
76
- # Create the query engine
77
- query_engine = index.as_query_engine(text_qa_template=text_qa_template)
78
-
79
- try:
80
- # Setup the TextIteratorStreamer for streaming the response
81
- streamer = TextIteratorStreamer(tokenizer, skip_special_tokens=True)
82
-
83
- # Create a thread to run the generation in the background
84
- def generate_response():
85
- query_engine.query(query_str, streamer=streamer)
86
-
87
- generation_thread = threading.Thread(target=generate_response)
88
- generation_thread.start()
89
-
90
- # Stream tokens as they are generated
91
- for new_text in streamer:
92
- yield new_text
93
- except Exception as e:
94
- yield f"Error processing query: {str(e)}"
95
 
96
- """ try:
97
- result = index.as_query_engine(text_qa_template=text_qa_template).query(query_str)
98
  response_text = result.response
99
 
100
  # Remove any unwanted tokens like <end_of_turn>
@@ -102,7 +79,7 @@ def handle_query(query_str, chathistory):
102
 
103
  yield cleaned_result
104
  except Exception as e:
105
- yield f"Error processing query: {str(e)}" """
106
 
107
 
108
 
 
13
  import spaces
14
  from huggingface_hub import login
15
 
 
 
 
16
 
17
  huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
18
  login(huggingface_token)
 
69
  ("user", qa_prompt_str),
70
  ]
71
  text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
+ try:
74
+ result = index.as_query_engine(text_qa_template=text_qa_template, streaming=True).query(query_str)
75
  response_text = result.response
76
 
77
  # Remove any unwanted tokens like <end_of_turn>
 
79
 
80
  yield cleaned_result
81
  except Exception as e:
82
+ yield f"Error processing query: {str(e)}"
83
 
84
 
85