Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,166 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
temperature=temperature,
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
# import gradio as gr
|
67 |
|
68 |
# gr.load("models/meta-llama/Meta-Llama-3.1-70B-Instruct").launch()
|
69 |
-
|
70 |
# import streamlit as st
|
71 |
# from transformers import AutoTokenizer, AutoModelForCausalLM
|
72 |
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from openai import OpenAI
|
3 |
+
import os
|
4 |
+
import numpy as np
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import openai
|
7 |
+
|
8 |
+
# Load environment variables
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Initialize the OpenAI client for OpenAI models
|
12 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
13 |
+
|
14 |
+
# Hugging Face API client setup (if needed)
|
15 |
+
HF_API_KEY = os.getenv("HF_API_KEY")
|
16 |
+
huggingface_url = "https://api-inference.huggingface.co/models/"
|
17 |
+
|
18 |
+
# Create supported models dictionary
|
19 |
+
model_links = {
|
20 |
+
"ChatGPT": "openai/gpt-4",
|
21 |
+
"Meta-Llama-3.1-70B-Instruct": "meta-llama/Meta-Llama-3.1-70B-Instruct",
|
22 |
+
"Mistral-7B-Instruct-v0.1": "mistralai/Mistral-7B-Instruct-v0.1",
|
23 |
+
# Add more models as needed
|
24 |
+
}
|
25 |
+
|
26 |
+
# Define functions to interact with OpenAI and Hugging Face
|
27 |
+
def query_openai(prompt, temperature):
|
28 |
+
"""Query OpenAI's GPT model."""
|
29 |
+
response = openai.ChatCompletion.create(
|
30 |
+
model="gpt-4",
|
31 |
+
messages=[{"role": "user", "content": prompt}],
|
|
|
|
|
32 |
temperature=temperature,
|
33 |
+
)
|
34 |
+
return response.choices[0].message['content']
|
35 |
+
|
36 |
+
def query_huggingface(prompt, model, temperature):
|
37 |
+
"""Query Hugging Face's API."""
|
38 |
+
headers = {"Authorization": f"Bearer {HF_API_KEY}"}
|
39 |
+
payload = {
|
40 |
+
"inputs": prompt,
|
41 |
+
"parameters": {"temperature": temperature, "return_full_text": False},
|
42 |
+
}
|
43 |
+
response = requests.post(f"{huggingface_url}{model}", headers=headers, json=payload)
|
44 |
+
return response.json()[0]['generated_text']
|
45 |
+
|
46 |
+
# Function to reset conversation
|
47 |
+
def reset_conversation():
|
48 |
+
st.session_state.messages = []
|
49 |
+
st.session_state.responses = []
|
50 |
+
st.session_state.current_model = None
|
51 |
+
|
52 |
+
# Sidebar setup
|
53 |
+
st.sidebar.title("ChatBot Configuration")
|
54 |
+
selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys()))
|
55 |
+
temperature = st.sidebar.slider("Temperature", 0.0, 1.0, 0.5)
|
56 |
+
|
57 |
+
# Reset chat button
|
58 |
+
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
59 |
+
|
60 |
+
# Initialize session state variables
|
61 |
+
if 'messages' not in st.session_state:
|
62 |
+
st.session_state.messages = []
|
63 |
+
if 'responses' not in st.session_state:
|
64 |
+
st.session_state.responses = []
|
65 |
+
if 'current_model' not in st.session_state:
|
66 |
+
st.session_state.current_model = selected_model
|
67 |
+
|
68 |
+
# Check if the model was changed
|
69 |
+
if st.session_state.current_model != selected_model:
|
70 |
+
reset_conversation()
|
71 |
+
st.session_state.current_model = selected_model
|
72 |
+
|
73 |
+
# Chat Interface
|
74 |
+
st.title(f"Chat with {selected_model}")
|
75 |
+
|
76 |
+
# Display previous chat messages
|
77 |
+
for message in st.session_state.messages:
|
78 |
+
with st.chat_message(message["role"]):
|
79 |
+
st.markdown(message["content"])
|
80 |
+
|
81 |
+
# Accept user input
|
82 |
+
if prompt := st.chat_input("Ask me anything..."):
|
83 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
84 |
+
|
85 |
+
with st.chat_message("assistant"):
|
86 |
+
if selected_model == "ChatGPT":
|
87 |
+
response = query_openai(prompt, temperature)
|
88 |
+
else:
|
89 |
+
response = query_huggingface(prompt, model_links[selected_model], temperature)
|
90 |
+
st.markdown(response)
|
91 |
+
|
92 |
+
st.session_state.responses.append(response)
|
93 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
94 |
|
95 |
|
96 |
+
# import gradio as gr
|
97 |
+
# from huggingface_hub import InferenceClient
|
98 |
+
|
99 |
+
# """
|
100 |
+
# 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
|
101 |
+
# """
|
102 |
+
# client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
103 |
+
|
104 |
+
|
105 |
+
# def respond(
|
106 |
+
# message,
|
107 |
+
# history: list[tuple[str, str]],
|
108 |
+
# system_message,
|
109 |
+
# max_tokens,
|
110 |
+
# temperature,
|
111 |
+
# top_p,
|
112 |
+
# ):
|
113 |
+
# messages = [{"role": "system", "content": system_message}]
|
114 |
+
|
115 |
+
# for val in history:
|
116 |
+
# if val[0]:
|
117 |
+
# messages.append({"role": "user", "content": val[0]})
|
118 |
+
# if val[1]:
|
119 |
+
# messages.append({"role": "assistant", "content": val[1]})
|
120 |
+
|
121 |
+
# messages.append({"role": "user", "content": message})
|
122 |
+
|
123 |
+
# response = ""
|
124 |
+
|
125 |
+
# for message in client.chat_completion(
|
126 |
+
# messages,
|
127 |
+
# max_tokens=max_tokens,
|
128 |
+
# stream=True,
|
129 |
+
# temperature=temperature,
|
130 |
+
# top_p=top_p,
|
131 |
+
# ):
|
132 |
+
# token = message.choices[0].delta.content
|
133 |
+
|
134 |
+
# response += token
|
135 |
+
# yield response
|
136 |
+
|
137 |
+
# """
|
138 |
+
# For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
139 |
+
# """
|
140 |
+
# demo = gr.ChatInterface(
|
141 |
+
# respond,
|
142 |
+
# additional_inputs=[
|
143 |
+
# gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
144 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
145 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
146 |
+
# gr.Slider(
|
147 |
+
# minimum=0.1,
|
148 |
+
# maximum=1.0,
|
149 |
+
# value=0.95,
|
150 |
+
# step=0.05,
|
151 |
+
# label="Top-p (nucleus sampling)",
|
152 |
+
# ),
|
153 |
+
# ],
|
154 |
+
# )
|
155 |
+
|
156 |
+
|
157 |
+
# if __name__ == "__main__":
|
158 |
+
# demo.launch()
|
159 |
+
#####################################
|
160 |
# import gradio as gr
|
161 |
|
162 |
# gr.load("models/meta-llama/Meta-Llama-3.1-70B-Instruct").launch()
|
163 |
+
########################################
|
164 |
# import streamlit as st
|
165 |
# from transformers import AutoTokenizer, AutoModelForCausalLM
|
166 |
|