Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,45 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
@author: idoia lerchundi
|
| 3 |
+
"""
|
| 4 |
+
import os
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from huggingface_hub import InferenceClient
|
| 7 |
+
|
| 8 |
+
# Load the API token from an environment variable
|
| 9 |
+
api_key = os.getenv("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
# Instantiate the InferenceClient
|
| 12 |
+
client = InferenceClient(api_key=api_key)
|
| 13 |
+
|
| 14 |
+
# Streamlit app title
|
| 15 |
+
st.title("Hugging Face Inference with Streamlit")
|
| 16 |
+
|
| 17 |
+
# Create a text input area for user prompts
|
| 18 |
+
with st.form("my_form"):
|
| 19 |
+
text = st.text_area("Enter text:", "Tell me a joke to make me laugh.")
|
| 20 |
+
submitted = st.form_submit_button("Submit")
|
| 21 |
+
|
| 22 |
+
# Initialize the full_text variable
|
| 23 |
+
full_text = ""
|
| 24 |
+
|
| 25 |
+
if submitted:
|
| 26 |
+
messages = [
|
| 27 |
+
{"role": "user", "content": text}
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
# Create a new stream for each submission
|
| 31 |
+
stream = client.chat.completions.create(
|
| 32 |
+
model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 33 |
+
messages=messages,
|
| 34 |
+
temperature=0.5,
|
| 35 |
+
max_tokens=100,
|
| 36 |
+
top_p=0.7,
|
| 37 |
+
stream=True
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Concatenate chunks to form the full response
|
| 41 |
+
for chunk in stream:
|
| 42 |
+
full_text += chunk.choices[0].delta.content
|
| 43 |
+
|
| 44 |
+
# Display the full response
|
| 45 |
+
st.info(full_text)
|