Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# Set Groq API Key
|
| 6 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 7 |
+
# Ensure the API key is set in the environment
|
| 8 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 9 |
+
if not api_key:
|
| 10 |
+
raise ValueError("GROQ_API_KEY is not set in the environment.")
|
| 11 |
+
client = Groq(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
# Function to generate response using Groq
|
| 14 |
+
def chat_with_llm(user_message, model="llama3-8b-8192"):
|
| 15 |
+
try:
|
| 16 |
+
# Send message to Groq LLM
|
| 17 |
+
chat_completion = client.chat.completions.create(
|
| 18 |
+
messages=[
|
| 19 |
+
{"role": "user", "content": user_message},
|
| 20 |
+
],
|
| 21 |
+
model=model,
|
| 22 |
+
)
|
| 23 |
+
# Return the LLM's response
|
| 24 |
+
return chat_completion.choices[0].message.content
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error: {e}"
|
| 27 |
+
|
| 28 |
+
# Streamlit App
|
| 29 |
+
def main():
|
| 30 |
+
st.title("Real-Time Text-to-Text Chatbot")
|
| 31 |
+
#st.write("Powered by Groq and deployed on Streamlit.")
|
| 32 |
+
|
| 33 |
+
# Input text from the user
|
| 34 |
+
user_input = st.text_input("You:", "")
|
| 35 |
+
|
| 36 |
+
# Display bot's response
|
| 37 |
+
if st.button("Send"):
|
| 38 |
+
if user_input.strip():
|
| 39 |
+
with st.spinner("Thinking..."):
|
| 40 |
+
bot_response = chat_with_llm(user_input)
|
| 41 |
+
st.text_area("Bot:", bot_response, height=150)
|
| 42 |
+
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
main()
|