Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,78 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
import os
|
@@ -96,3 +170,5 @@ if user_input:
|
|
96 |
# Update session state
|
97 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
98 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
load_dotenv()
|
8 |
+
api_key = os.getenv("api_key")
|
9 |
+
|
10 |
+
# App title and description
|
11 |
+
st.title("I am Your GrowBuddy 🌱")
|
12 |
+
st.write("Let me help you start gardening. Let's grow together!")
|
13 |
+
|
14 |
+
# Initialize Hugging Face InferenceClient
|
15 |
+
model_name = "unsloth/gemma-2-2b" # Use the appropriate model
|
16 |
+
client = InferenceClient(model=model_name, token=api_key)
|
17 |
+
|
18 |
+
# Initialize session state messages
|
19 |
+
if "messages" not in st.session_state:
|
20 |
+
st.session_state.messages = [
|
21 |
+
{"role": "assistant", "content": "Hello there! How can I help you with gardening today?"}
|
22 |
+
]
|
23 |
+
|
24 |
+
# Display conversation history
|
25 |
+
for message in st.session_state.messages:
|
26 |
+
with st.chat_message(message["role"]):
|
27 |
+
st.write(message["content"])
|
28 |
+
|
29 |
+
# Create a text area to display logs
|
30 |
+
log_box = st.empty()
|
31 |
+
|
32 |
+
# Function to generate response using Hugging Face's remote model
|
33 |
+
def generate_response(prompt):
|
34 |
+
try:
|
35 |
+
log_box.text_area("Debugging Logs", "Generating output...", height=200)
|
36 |
+
|
37 |
+
# Generate output from the Hugging Face API
|
38 |
+
response = client.text_generation(prompt, max_new_tokens=100, temperature=0.7, do_sample=True)
|
39 |
+
|
40 |
+
# Print and log the response to understand the structure
|
41 |
+
log_box.text_area("Debugging Logs", f"Response: {response}", height=200)
|
42 |
+
|
43 |
+
# Check for proper response structure
|
44 |
+
if isinstance(response, list) and len(response) > 0 and "generated_text" in response[0]:
|
45 |
+
output_text = response[0]["generated_text"]
|
46 |
+
else:
|
47 |
+
raise ValueError("Unexpected response structure from Hugging Face API.")
|
48 |
+
|
49 |
+
log_box.text_area("Debugging Logs", f"Decoded response: {output_text}", height=200)
|
50 |
+
return output_text
|
51 |
+
except Exception as e:
|
52 |
+
st.error(f"Error during text generation: {str(e)}")
|
53 |
+
log_box.text_area("Error Details", str(e), height=200)
|
54 |
+
return "Sorry, I couldn't process your request."
|
55 |
+
|
56 |
+
|
57 |
+
# User input field for gardening questions
|
58 |
+
user_input = st.chat_input("Type your gardening question here:")
|
59 |
+
|
60 |
+
if user_input:
|
61 |
+
with st.chat_message("user"):
|
62 |
+
st.write(user_input)
|
63 |
+
|
64 |
+
with st.chat_message("assistant"):
|
65 |
+
with st.spinner("Generating your answer..."):
|
66 |
+
response = generate_response(user_input)
|
67 |
+
st.write(response)
|
68 |
+
|
69 |
+
# Update session state
|
70 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
71 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
72 |
+
|
73 |
+
|
74 |
+
'''
|
75 |
+
import streamlit as st
|
76 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
77 |
import torch
|
78 |
import os
|
|
|
170 |
# Update session state
|
171 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
172 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
173 |
+
|
174 |
+
'''
|