Hev832 commited on
Commit
5e56d27
1 Parent(s): 4b65736

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -23
app.py CHANGED
@@ -1,26 +1,77 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
-
5
- # Load the model and tokenizer
6
- model_name = "indonlp/cendol-llama2-7b-chat"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
8
- model = AutoModelForCausalLM.from_pretrained(model_name)
9
-
10
- # Define the chatbot function
11
- def chatbot(input_text):
12
- inputs = tokenizer(input_text, return_tensors="pt")
13
- with torch.no_grad():
14
- outputs = model.generate(inputs.input_ids, max_length=1000, do_sample=True)
15
- response = tokenizer.decode(outputs[0], skip_special_tokens=True)
16
- return response
17
-
18
- # Create the Gradio interface
19
- interface = gr.ChatInterface(
20
- fn=chatbot,
21
- title="Cendol Chatnot",
22
- description="Ask any coding-related questions and get helpful responses."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  )
24
 
25
- # Launch the web UI
26
- interface.launch(share=True)
 
 
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()