Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,24 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def respond(
|
11 |
message,
|
@@ -17,6 +30,12 @@ def respond(
|
|
17 |
):
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
for val in history:
|
21 |
if val[0]:
|
22 |
messages.append({"role": "user", "content": val[0]})
|
@@ -35,13 +54,19 @@ def respond(
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
38 |
-
|
39 |
response += token
|
40 |
yield response
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
demo = gr.ChatInterface(
|
46 |
respond,
|
47 |
additional_inputs=[
|
@@ -58,6 +83,5 @@ demo = gr.ChatInterface(
|
|
58 |
],
|
59 |
)
|
60 |
|
61 |
-
|
62 |
if __name__ == "__main__":
|
63 |
demo.launch()
|
|
|
1 |
+
import os
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import InferenceClient
|
4 |
+
import markdown
|
5 |
|
|
|
|
|
|
|
6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
7 |
|
8 |
+
# Function to read and process Markdown files from the 'data' directory
|
9 |
+
def load_markdown_files(data_folder='data'):
|
10 |
+
documents = []
|
11 |
+
for filename in os.listdir(data_folder):
|
12 |
+
if filename.endswith('.md'):
|
13 |
+
with open(os.path.join(data_folder, filename), 'r', encoding='utf-8') as file:
|
14 |
+
content = file.read()
|
15 |
+
# Convert Markdown to plain text if needed
|
16 |
+
html_content = markdown.markdown(content)
|
17 |
+
documents.append(html_content) # Store HTML content or plain text
|
18 |
+
return documents
|
19 |
+
|
20 |
+
# Load documents at startup
|
21 |
+
documents = load_markdown_files()
|
22 |
|
23 |
def respond(
|
24 |
message,
|
|
|
30 |
):
|
31 |
messages = [{"role": "system", "content": system_message}]
|
32 |
|
33 |
+
# Retrieve relevant context from loaded documents based on the user message
|
34 |
+
relevant_contexts = retrieve_relevant_context(message, documents)
|
35 |
+
|
36 |
+
# Add retrieved contexts to the messages for better responses
|
37 |
+
messages.append({"role": "context", "content": " ".join(relevant_contexts)})
|
38 |
+
|
39 |
for val in history:
|
40 |
if val[0]:
|
41 |
messages.append({"role": "user", "content": val[0]})
|
|
|
54 |
top_p=top_p,
|
55 |
):
|
56 |
token = message.choices[0].delta.content
|
|
|
57 |
response += token
|
58 |
yield response
|
59 |
|
60 |
+
def retrieve_relevant_context(query, documents):
|
61 |
+
# Simple keyword matching to find relevant documents
|
62 |
+
relevant_contexts = []
|
63 |
+
|
64 |
+
for doc in documents:
|
65 |
+
if query.lower() in doc.lower(): # Basic keyword search (case insensitive)
|
66 |
+
relevant_contexts.append(doc)
|
67 |
+
|
68 |
+
return relevant_contexts[:3] # Return top 3 relevant contexts
|
69 |
+
|
70 |
demo = gr.ChatInterface(
|
71 |
respond,
|
72 |
additional_inputs=[
|
|
|
83 |
],
|
84 |
)
|
85 |
|
|
|
86 |
if __name__ == "__main__":
|
87 |
demo.launch()
|