xingshining commited on
Commit
fd7a12e
·
1 Parent(s): de0f219

初始化项目

Browse files
Files changed (2) hide show
  1. app.py +141 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from phi.assistant import Assistant
3
+ from phi.llm.openai import OpenAIChat
4
+ from phi.llm.groq import Groq
5
+ from phi.llm.ollama import Ollama
6
+ from phi.tools.duckduckgo import DuckDuckGo
7
+
8
+ # 模型字典
9
+ model_choices = {
10
+ "OpenAI Model (gpt-3.5-turbo)": {
11
+ "class": OpenAIChat,
12
+ "params": {
13
+ "model": "gpt-3.5-turbo"
14
+ }
15
+ },
16
+ "Groq Model (llama3-70b-8192)": {
17
+ "class": Groq,
18
+ "params": {
19
+ "model": "llama3-70b-8192"
20
+ }
21
+ },
22
+ "Ollama Model (llama3)": {
23
+ "class": Ollama,
24
+ "params": {
25
+ "model": "llama3"
26
+ }
27
+ }
28
+ }
29
+
30
+ def ai_search(api_key, model_choice, base_url, query):
31
+ if model_choice in ["OpenAI Model (gpt-3.5-turbo)", "Groq Model (llama3-70b-8192)"] and not api_key:
32
+ return "API key is required."
33
+
34
+ if model_choice == "OpenAI Model (gpt-3.5-turbo)" and not base_url:
35
+ return "Base URL is required for OpenAI Model."
36
+
37
+ # 动态创建模型实例
38
+ model_info = model_choices[model_choice]
39
+ llm_class = model_info["class"]
40
+ llm_params = model_info["params"]
41
+
42
+ if model_choice != "Ollama Model (llama3)":
43
+ llm_params["api_key"] = api_key
44
+
45
+ if model_choice == "OpenAI Model (gpt-3.5-turbo)":
46
+ llm_params["base_url"] = base_url
47
+
48
+ llm_instance = llm_class(**llm_params)
49
+
50
+ # 创建 Assistant 实例
51
+ assistant = Assistant(llm=llm_instance, tools=[DuckDuckGo()], show_tool_calls=True)
52
+
53
+ # 使用 DuckDuckGo 进行搜索
54
+ search_results_generator = assistant.run(f"Search: {query}")
55
+
56
+ if not search_results_generator:
57
+ return "No results found."
58
+
59
+ # 迭代生成器并合并结果
60
+ search_results = "".join(list(search_results_generator))
61
+
62
+ # 打印搜索结果以检查其结构
63
+ print(search_results)
64
+
65
+ # 格式化搜索结果为 Markdown
66
+ markdown_results = f"### Search Results for: {query}\n\n"
67
+ markdown_results += "Here are some results:\n\n"
68
+ markdown_results += f"{search_results}\n"
69
+
70
+ return markdown_results
71
+
72
+ def create_interface():
73
+ with gr.Blocks() as iface:
74
+ gr.Markdown("## AI Search Engine")
75
+
76
+ model_choice_input = gr.Dropdown(label="Choose Model", choices=list(model_choices.keys()))
77
+ api_key_input = gr.Textbox(label="API Key", placeholder="Enter your API key here", type="password", visible=False)
78
+ base_url_input = gr.Textbox(label="Base URL", placeholder="Enter the base URL here", visible=False)
79
+ query_input = gr.Textbox(label="Search Query", placeholder="Enter your search query here")
80
+
81
+ search_button = gr.Button("Search", interactive=False)
82
+
83
+ output = gr.Textbox(label="Response", interactive=False)
84
+
85
+ def update_inputs(model_choice):
86
+ if model_choice == "OpenAI Model (gpt-3.5-turbo)":
87
+ return gr.update(visible=True), gr.update(visible=True), "", ""
88
+ elif model_choice == "Groq Model (llama3-70b-8192)":
89
+ return gr.update(visible=True), gr.update(visible=False), "", ""
90
+ else:
91
+ return gr.update(visible=False), gr.update(visible=False), "", ""
92
+
93
+ def check_button_state(api_key, base_url, model_choice, query):
94
+ print(api_key)
95
+ print(base_url)
96
+ print(model_choice)
97
+ if model_choice == "Ollama Model (llama3)":
98
+ return gr.update(interactive=bool(query))
99
+ elif model_choice == "Groq Model (llama3-70b-8192)":
100
+ return gr.update(interactive=True)
101
+ elif model_choice == "OpenAI Model (gpt-3.5-turbo)":
102
+ return gr.update(interactive=bool(api_key and base_url and query))
103
+ else:
104
+ return gr.update(interactive=True)
105
+
106
+ def combined_update(model_choice, api_key, base_url, query):
107
+ updates = update_inputs(model_choice)
108
+ button_state = check_button_state(api_key, base_url, model_choice, query)
109
+ return updates + (button_state,)
110
+
111
+ model_choice_input.change(
112
+ lambda model_choice: combined_update(model_choice, api_key_input.value, base_url_input.value, query_input.value),
113
+ inputs=model_choice_input,
114
+ outputs=[api_key_input, base_url_input, api_key_input, base_url_input, search_button]
115
+ )
116
+ api_key_input.change(
117
+ lambda api_key: check_button_state(api_key, base_url_input.value, model_choice_input.value, query_input.value),
118
+ inputs=api_key_input,
119
+ outputs=search_button
120
+ )
121
+ base_url_input.change(
122
+ lambda base_url: check_button_state(api_key_input.value, base_url, model_choice_input.value, query_input.value),
123
+ inputs=base_url_input,
124
+ outputs=search_button
125
+ )
126
+ query_input.change(
127
+ lambda query: check_button_state(api_key_input.value, base_url_input.value, model_choice_input.value, query),
128
+ inputs=query_input,
129
+ outputs=search_button
130
+ )
131
+
132
+ search_button.click(
133
+ fn=ai_search,
134
+ inputs=[api_key_input, model_choice_input, base_url_input, query_input],
135
+ outputs=output
136
+ )
137
+
138
+ return iface
139
+
140
+ iface = create_interface()
141
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio==4.39.0