Spaces:
Sleeping
Sleeping
BlooperDodge
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import requests
|
|
6 |
from bs4 import BeautifulSoup
|
7 |
import os
|
8 |
import gradio as gr
|
|
|
9 |
|
10 |
# Step 1: Define PromptTemplate class using LangChain's format
|
11 |
class PromptTemplate:
|
@@ -109,22 +110,20 @@ def extract_content(url):
|
|
109 |
|
110 |
# Step 8: Use the LangChain text generation pipeline for generating answers
|
111 |
generation_model_name = "microsoft/Phi-3-mini-4k-instruct"
|
112 |
-
|
|
|
113 |
|
114 |
# Step 9: Function to generate answer based on query and content
|
115 |
-
def generate_answer(query, contents):
|
116 |
answers = []
|
117 |
prompt_template = PromptTemplate("""
|
118 |
|
119 |
### Medical Assistant Context ###
|
120 |
As a helpful medical assistant, I'm here to assist you with your query.
|
121 |
-
|
122 |
### Medical Query ###
|
123 |
Query: {query}
|
124 |
-
|
125 |
### Explanation ###
|
126 |
{generated_text}
|
127 |
-
|
128 |
### Revised Response ###
|
129 |
Response: {generated_text}
|
130 |
""")
|
@@ -138,8 +137,8 @@ Response: {generated_text}
|
|
138 |
if not batch_prompts:
|
139 |
return ["No content available to generate an answer."] * len(contents)
|
140 |
|
141 |
-
# Generate responses in batch
|
142 |
-
generated_texts = text_generator(
|
143 |
|
144 |
for i, generated_text in enumerate(generated_texts):
|
145 |
if generated_text and isinstance(generated_text, list) and len(generated_text) > 0:
|
@@ -156,7 +155,7 @@ def process_query(query):
|
|
156 |
top_results = search_similar(query, top_k=3)
|
157 |
if top_results:
|
158 |
content = extract_content(top_results[0])
|
159 |
-
answer = generate_answer(query, [content])[0]
|
160 |
|
161 |
response = f"Rank 1: URL - {top_results[0]}\n"
|
162 |
response += f"Generated Answer:\n{answer}\n"
|
@@ -176,4 +175,5 @@ demo = gr.Interface(
|
|
176 |
)
|
177 |
|
178 |
if __name__ == "__main__":
|
|
|
179 |
demo.launch()
|
|
|
6 |
from bs4 import BeautifulSoup
|
7 |
import os
|
8 |
import gradio as gr
|
9 |
+
import asyncio # Import asyncio for asynchronous processing
|
10 |
|
11 |
# Step 1: Define PromptTemplate class using LangChain's format
|
12 |
class PromptTemplate:
|
|
|
110 |
|
111 |
# Step 8: Use the LangChain text generation pipeline for generating answers
|
112 |
generation_model_name = "microsoft/Phi-3-mini-4k-instruct"
|
113 |
+
# Use CPU or change to device=0 for GPU (depending on your setup)
|
114 |
+
text_generator = pipeline("text-generation", model=generation_model_name, device=-1)
|
115 |
|
116 |
# Step 9: Function to generate answer based on query and content
|
117 |
+
async def generate_answer(query, contents):
|
118 |
answers = []
|
119 |
prompt_template = PromptTemplate("""
|
120 |
|
121 |
### Medical Assistant Context ###
|
122 |
As a helpful medical assistant, I'm here to assist you with your query.
|
|
|
123 |
### Medical Query ###
|
124 |
Query: {query}
|
|
|
125 |
### Explanation ###
|
126 |
{generated_text}
|
|
|
127 |
### Revised Response ###
|
128 |
Response: {generated_text}
|
129 |
""")
|
|
|
137 |
if not batch_prompts:
|
138 |
return ["No content available to generate an answer."] * len(contents)
|
139 |
|
140 |
+
# Generate responses in batch asynchronously
|
141 |
+
generated_texts = await asyncio.gather(*[loop.run_in_executor(None, lambda: text_generator(prompt, max_new_tokens=200, num_return_sequences=1, truncation=True)) for prompt in batch_prompts])
|
142 |
|
143 |
for i, generated_text in enumerate(generated_texts):
|
144 |
if generated_text and isinstance(generated_text, list) and len(generated_text) > 0:
|
|
|
155 |
top_results = search_similar(query, top_k=3)
|
156 |
if top_results:
|
157 |
content = extract_content(top_results[0])
|
158 |
+
answer = asyncio.run(generate_answer(query, [content]))[0]
|
159 |
|
160 |
response = f"Rank 1: URL - {top_results[0]}\n"
|
161 |
response += f"Generated Answer:\n{answer}\n"
|
|
|
175 |
)
|
176 |
|
177 |
if __name__ == "__main__":
|
178 |
+
loop = asyncio.get_event_loop()
|
179 |
demo.launch()
|