Shrees0507 commited on
Commit
8b24140
·
verified ·
1 Parent(s): 4c81888

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -21
app.py CHANGED
@@ -1,27 +1,32 @@
1
- import os
2
- from openai import OpenAI
3
  import gradio as gr
 
 
4
 
5
- api_key=os.environ.get("OPEN_API_KEY")
6
- client = OpenAI(api_key=api_key)
7
 
8
- def predict(message, history):
9
- history_openai_format = []
10
- for human, assistant in history:
11
- history_openai_format.append({"role": "user", "content": human })
12
- history_openai_format.append({"role": "assistant", "content":assistant})
13
- history_openai_format.append({"role": "user", "content": message})
14
-
15
- response = client.chat.completions.create(model='gpt-3.5-turbo',
16
- messages= history_openai_format,
17
- temperature=1.0,
18
- stream=True)
19
 
20
- partial_message = ""
21
- for chunk in response:
22
- if chunk.choices[0].delta.content is not None:
23
- partial_message = partial_message + chunk.choices[0].delta.content
24
- yield partial_message
25
 
26
- gr.ChatInterface(predict).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
 
 
 
 
1
+ from huggingface_hub import InferenceClient
 
2
  import gradio as gr
3
+ from dotenv import load_dotenv
4
+ import os
5
 
6
+ load_dotenv()
 
7
 
8
+ client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HF_TOKEN"))
 
 
 
 
 
 
 
 
 
 
9
 
10
+ #built a chat interface on gradio
 
 
 
 
11
 
12
+ def chatbot(input, history):
13
+ message = [{"role": "user", "content": input}]
14
+ #print(message)
15
+ history = [{"role" : "system", "content": "You are a helpful assistant."}]
16
+ #print(history)
17
+ messages = history + message
18
+ #print(messages)
19
+ output = client.chat_completion(
20
+ messages=messages,
21
+ max_tokens=256,
22
+ temperature=0.7
23
+ )
24
+ #print(output)
25
+ history = history + [{"role": "assistant", "content": output.choices[0].message.content}]
26
+ #print(history)
27
+ #print(history[0])
28
+ return output.choices[0].message.content
29
 
30
+ demo = gr.ChatInterface(chatbot, title="Chatbot",fill_height=True)
31
+
32
+ demo.launch()