aguyader commited on
Commit
23d49ab
·
verified ·
1 Parent(s): f307bb6

Update app2.py

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