Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,98 +1,92 @@
|
|
|
|
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
|
12 |
-
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
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 |
-
"
|
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 |
-
|
54 |
-
selected_model = st.sidebar.selectbox("Select Model",
|
55 |
-
|
56 |
-
|
57 |
-
# Reset chat button
|
58 |
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
if
|
66 |
-
st.session_state.
|
67 |
|
68 |
-
|
69 |
-
|
|
|
70 |
reset_conversation()
|
71 |
-
st.session_state.current_model = selected_model
|
72 |
|
73 |
-
#
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
# Display
|
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("
|
|
|
|
|
83 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
84 |
|
85 |
with st.chat_message("assistant"):
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
1 |
+
import numpy as np
|
2 |
import streamlit as st
|
3 |
from openai import OpenAI
|
4 |
import os
|
|
|
5 |
from dotenv import load_dotenv
|
|
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
9 |
|
10 |
+
# Initialize the OpenAI client
|
11 |
+
client = OpenAI(
|
12 |
+
base_url="https://api-inference.huggingface.co/v1",
|
13 |
+
api_key=os.environ.get('API_KEY') # Replace with your token
|
14 |
+
)
|
15 |
|
16 |
+
# Define model links
|
|
|
|
|
|
|
|
|
17 |
model_links = {
|
|
|
18 |
"Meta-Llama-3.1-70B-Instruct": "meta-llama/Meta-Llama-3.1-70B-Instruct",
|
19 |
+
"Meta-Llama-3.1-8B-Instruct": "meta-llama/Meta-Llama-3.1-8B-Instruct",
|
20 |
# Add more models as needed
|
21 |
}
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
# Function to reset conversation
|
24 |
def reset_conversation():
|
25 |
+
st.session_state.conversation = []
|
26 |
st.session_state.messages = []
|
|
|
|
|
27 |
|
28 |
# Sidebar setup
|
29 |
+
models = [key for key in model_links.keys()]
|
30 |
+
selected_model = st.sidebar.selectbox("Select Model", models)
|
31 |
+
temp_values = st.sidebar.slider('Select a temperature value', 0.0, 1.0, 0.5)
|
|
|
|
|
32 |
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
33 |
|
34 |
+
st.sidebar.write(f"You're now chatting with **{selected_model}**")
|
35 |
+
st.sidebar.markdown("*Generated content may be inaccurate or false.*")
|
36 |
+
st.sidebar.markdown("\n[TypeGPT](https://typegpt.net).")
|
37 |
+
|
38 |
+
# Manage session state
|
39 |
+
if "prev_option" not in st.session_state:
|
40 |
+
st.session_state.prev_option = selected_model
|
41 |
|
42 |
+
if st.session_state.prev_option != selected_model:
|
43 |
+
st.session_state.messages = []
|
44 |
+
st.session_state.prev_option = selected_model
|
45 |
reset_conversation()
|
|
|
46 |
|
47 |
+
# Model repository id
|
48 |
+
repo_id = model_links[selected_model]
|
49 |
+
|
50 |
+
# Main chat interface
|
51 |
+
st.subheader(f'TypeGPT.net - {selected_model}')
|
52 |
+
|
53 |
+
# Initialize chat history
|
54 |
+
if "messages" not in st.session_state:
|
55 |
+
st.session_state.messages = []
|
56 |
|
57 |
+
# Display chat messages from history on app rerun
|
58 |
for message in st.session_state.messages:
|
59 |
with st.chat_message(message["role"]):
|
60 |
st.markdown(message["content"])
|
61 |
|
62 |
# Accept user input
|
63 |
+
if prompt := st.chat_input(f"Hi I'm {selected_model}, ask me a question"):
|
64 |
+
with st.chat_message("user"):
|
65 |
+
st.markdown(prompt)
|
66 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
67 |
|
68 |
with st.chat_message("assistant"):
|
69 |
+
try:
|
70 |
+
stream = client.chat.completions.create(
|
71 |
+
model=model_links[selected_model],
|
72 |
+
messages=[
|
73 |
+
{"role": m["role"], "content": m["content"]}
|
74 |
+
for m in st.session_state.messages
|
75 |
+
],
|
76 |
+
temperature=temp_values,
|
77 |
+
stream=True,
|
78 |
+
max_tokens=3000,
|
79 |
+
)
|
80 |
+
response = st.write_stream(stream)
|
81 |
+
except Exception as e:
|
82 |
+
response = "😵💫 Looks like something went wrong! Please try again later."
|
83 |
+
st.write(response)
|
84 |
|
|
|
85 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
86 |
|
87 |
|
88 |
+
|
89 |
+
|
90 |
# import gradio as gr
|
91 |
# from huggingface_hub import InferenceClient
|
92 |
|