Loversofdeath commited on
Commit
9c8a7ac
·
verified ·
1 Parent(s): 9b01d01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -42
app.py CHANGED
@@ -1,50 +1,33 @@
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,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
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]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
  response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
  demo = gr.ChatInterface(
47
- respond,
48
  additional_inputs=[
49
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
@@ -59,6 +42,5 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
 
2
 
3
+ # Загрузка текстов из файлов лора
4
+ def load_lore_files():
5
+ lore_data = {}
6
+ for filename in ["vampires.txt", "kings.txt", "castles.txt"]:
7
+ with open(filename, "r", encoding="utf-8") as file:
8
+ lore_data[filename] = file.read()
9
+ return lore_data
10
 
11
+ lore_data = load_lore_files()
12
 
13
+ # Функция, которая ищет упоминания в лоре
14
+ def chatbot(message, history, system_message, max_tokens, temperature, top_p):
15
+ # Простой поиск по всем файлам лора
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  response = ""
17
+ for filename, text in lore_data.items():
18
+ if message.lower() in text.lower():
19
+ response += f"В файле {filename} есть что-то похожее на ваш запрос!\n"
20
+ if response == "":
21
+ return "Извините, ничего не нашёл по вашему запросу."
22
+
23
+ # Если есть ответ, добавляем его в историю
24
+ history.append((message, response))
25
+ return response, history
26
+
27
+
28
+ # Интерфейс чата с Gradio
 
 
 
 
 
29
  demo = gr.ChatInterface(
30
+ chatbot,
31
  additional_inputs=[
32
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
33
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
 
42
  ],
43
  )
44
 
 
45
  if __name__ == "__main__":
46
  demo.launch()