Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,26 +1,77 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
)
|
24 |
|
25 |
-
|
26 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from huggingface_hub import InferenceClient
|
4 |
+
import requests
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
|
7 |
+
# Initialize the text generation pipeline
|
8 |
+
pipe = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
|
9 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
10 |
+
|
11 |
+
def web_search(query):
|
12 |
+
# Simulate a web search using Google
|
13 |
+
response = requests.get(f"https://www.google.com/search?q={query}")
|
14 |
+
soup = BeautifulSoup(response.text, "html.parser")
|
15 |
+
results = []
|
16 |
+
for g in soup.find_all('div', class_='BNeawe vvjwJb AP7Wnd'):
|
17 |
+
results.append(g.get_text())
|
18 |
+
return results
|
19 |
+
|
20 |
+
def respond(
|
21 |
+
message,
|
22 |
+
history: list[tuple[str, str]],
|
23 |
+
system_message,
|
24 |
+
max_tokens,
|
25 |
+
temperature,
|
26 |
+
top_p,
|
27 |
+
):
|
28 |
+
messages = [{"role": "system", "content": system_message}]
|
29 |
+
|
30 |
+
for val in history:
|
31 |
+
if val[0]:
|
32 |
+
messages.append({"role": "user", "content": val[0]})
|
33 |
+
if val[1]:
|
34 |
+
messages.append({"role": "assistant", "content": val[1]})
|
35 |
+
|
36 |
+
# Check if message is a search request
|
37 |
+
if "search:" in message.lower():
|
38 |
+
search_query = message.split("search:", 1)[1].strip()
|
39 |
+
search_results = web_search(search_query)
|
40 |
+
response = "\n".join(search_results[:5]) # Return top 5 search results
|
41 |
+
else:
|
42 |
+
messages.append({"role": "user", "content": message})
|
43 |
+
response = ""
|
44 |
+
for message in client.chat_completion(
|
45 |
+
messages,
|
46 |
+
max_tokens=max_tokens,
|
47 |
+
stream=True,
|
48 |
+
temperature=temperature,
|
49 |
+
top_p=top_p,
|
50 |
+
):
|
51 |
+
token = message.choices[0].delta.content
|
52 |
+
response += token
|
53 |
+
yield response
|
54 |
+
|
55 |
+
yield response
|
56 |
+
|
57 |
+
|
58 |
+
demo = gr.ChatInterface(
|
59 |
+
respond,
|
60 |
+
title="INDONESIAN CHATBOT"
|
61 |
+
additional_inputs=[
|
62 |
+
gr.Textbox(value="You are a friendly AI Assistens Speak in indonesian", label="System message", visible=False),
|
63 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
64 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
65 |
+
gr.Slider(
|
66 |
+
minimum=0.1,
|
67 |
+
maximum=1.0,
|
68 |
+
value=0.95,
|
69 |
+
step=0.05,
|
70 |
+
label="Top-p (nucleus sampling)",
|
71 |
+
),
|
72 |
+
],
|
73 |
)
|
74 |
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
demo.launch()
|