Update app2.py
Browse files
app2.py
CHANGED
|
@@ -1,87 +1,90 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
#
|
| 43 |
-
#
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
gr.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
"
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
gr.
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
"""
|
|
|
|
| 1 |
+
pip install openai
|
| 2 |
+
pip install gradio
|
| 3 |
+
|
| 4 |
+
import openai
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import docx # For reading Word documents
|
| 7 |
+
|
| 8 |
+
openai.api_key = "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"
|
| 9 |
+
|
| 10 |
+
messages = [
|
| 11 |
+
{"role": "system", "content": "You are a chatbot that is very happy and kind. You will search the website https://www.patch-and-sparks.com/en/"},
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def read_word_document(file_path):
|
| 16 |
+
"""Read and return all paragraphs from the Word document."""
|
| 17 |
+
doc = docx.Document(file_path)
|
| 18 |
+
paragraphs = []
|
| 19 |
+
for paragraph in doc.paragraphs:
|
| 20 |
+
paragraphs.append(paragraph.text)
|
| 21 |
+
return paragraphs
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def search_in_doc(paragraphs, search_term):
|
| 25 |
+
"""Search for the term in each paragraph of the document and return the matching paragraph."""
|
| 26 |
+
for paragraph in paragraphs:
|
| 27 |
+
if search_term.lower() in paragraph.lower():
|
| 28 |
+
return f"I found this information in the document: '{paragraph}'"
|
| 29 |
+
return "Sorry, I couldn't find that information in the document."
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def chatbot(input):
|
| 34 |
+
if input:
|
| 35 |
+
# Add user input to the chat
|
| 36 |
+
messages.append({"role": "user", "content": input})
|
| 37 |
+
|
| 38 |
+
# Search for the information in the Word document
|
| 39 |
+
doc_content = read_word_document("./Info_1.docx") # Specify the path to your Word document
|
| 40 |
+
doc_reply = search_in_doc(doc_content, input)
|
| 41 |
+
|
| 42 |
+
## Get the chatbot response from OpenAI
|
| 43 |
+
#chat = openai.ChatCompletion.create(
|
| 44 |
+
# model="gpt-4", messages=messages
|
| 45 |
+
#)
|
| 46 |
+
#ai_reply = chat.choices[0].message.content
|
| 47 |
+
#messages.append({"role": "assistant", "content": ai_reply})
|
| 48 |
+
|
| 49 |
+
# Combine the response from OpenAI and the document search
|
| 50 |
+
#combined_reply = f"{ai_reply}\n\n{doc_reply}"
|
| 51 |
+
combined_reply = f"{doc_reply}"
|
| 52 |
+
return combined_reply
|
| 53 |
+
|
| 54 |
+
inputs = gr.Textbox(lines=7, label="Chat with AI")
|
| 55 |
+
outputs = gr.Textbox(label="Reply")
|
| 56 |
+
|
| 57 |
+
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="The AI Patch & Sparks Chatbot",
|
| 58 |
+
description="Ask anything you want about Patch&Sparks",
|
| 59 |
+
theme="compact").launch(share=True)
|
| 60 |
+
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
import openai
|
| 64 |
+
import gradio as gr
|
| 65 |
+
|
| 66 |
+
openai.api_key = "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"
|
| 67 |
+
|
| 68 |
+
messages = [
|
| 69 |
+
{"role": "system", "content": "You are a chatbot that is very happy and kind. You will search the website https://www.patch-and-sparks.com/en/"},
|
| 70 |
+
]
|
| 71 |
+
|
| 72 |
+
def chatbot(input):
|
| 73 |
+
if input:
|
| 74 |
+
messages.append({"role": "user", "content": input})
|
| 75 |
+
chat = openai.ChatCompletion.create(
|
| 76 |
+
#model="gpt-3.5-turbo", messages=messages
|
| 77 |
+
model="gpt-4o", messages=messages
|
| 78 |
+
)
|
| 79 |
+
reply = chat.choices[0].message.content
|
| 80 |
+
messages.append({"role": "assistant", "content": reply})
|
| 81 |
+
return reply
|
| 82 |
+
|
| 83 |
+
inputs = gr.Textbox(lines=7, label="Chat with AI")
|
| 84 |
+
outputs = gr.Textbox(label="Reply")
|
| 85 |
+
|
| 86 |
+
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="The AI Patch & Sparks Chatbot",
|
| 87 |
+
description="Ask anything you want about Patch&Sparks - - - -yeah",
|
| 88 |
+
theme="compact").launch(share=True)
|
| 89 |
+
|
| 90 |
"""
|