Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,48 @@
|
|
| 1 |
-
|
| 2 |
import gradio as gr
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def simple_chat_function(message, history):
|
| 6 |
-
"""
|
| 7 |
if not message.strip():
|
| 8 |
return "", history
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Add to history
|
| 14 |
-
history.append([message,
|
| 15 |
|
| 16 |
return "", history
|
| 17 |
|
|
@@ -25,4 +56,31 @@ with gr.Blocks() as demo:
|
|
| 25 |
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 26 |
|
| 27 |
if __name__ == "__main__":
|
| 28 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from phi.agent import Agent
|
| 3 |
+
from phi.model.groq import Groq
|
| 4 |
+
import os
|
| 5 |
+
import logging
|
| 6 |
+
|
| 7 |
+
# Set up logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
|
| 11 |
+
# API Key setup
|
| 12 |
+
api_key = os.getenv("GROQ_API_KEY")
|
| 13 |
+
if not api_key:
|
| 14 |
+
gr.Warning("GROQ_API_KEY not found. Set it in 'Repository secrets'.")
|
| 15 |
+
logger.error("GROQ_API_KEY not found.")
|
| 16 |
+
api_key = "" # Fallback to empty string, but this will fail without a key
|
| 17 |
+
else:
|
| 18 |
+
os.environ["GROQ_API_KEY"] = api_key
|
| 19 |
+
|
| 20 |
+
# Initialize PhiData Agent
|
| 21 |
+
agent = Agent(
|
| 22 |
+
model=Groq(model="llama3-70b-8192", api_key=api_key),
|
| 23 |
+
instructions=[
|
| 24 |
+
"You are a helpful assistant designed to answer questions on various topics.",
|
| 25 |
+
"Provide concise and accurate responses.",
|
| 26 |
+
"If you don't know the answer, say 'I don’t have enough information to answer that.'"
|
| 27 |
+
],
|
| 28 |
+
markdown=True
|
| 29 |
+
)
|
| 30 |
|
| 31 |
def simple_chat_function(message, history):
|
| 32 |
+
"""Chat function with PhiData agent integration"""
|
| 33 |
if not message.strip():
|
| 34 |
return "", history
|
| 35 |
|
| 36 |
+
# Generate response using PhiData agent
|
| 37 |
+
try:
|
| 38 |
+
response = agent.run(message)
|
| 39 |
+
response_text = response.content if hasattr(response, 'content') else "Error generating response."
|
| 40 |
+
except Exception as e:
|
| 41 |
+
logger.error(f"Agent error: {e}")
|
| 42 |
+
response_text = "Sorry, there was an error processing your request."
|
| 43 |
+
|
| 44 |
# Add to history
|
| 45 |
+
history.append([message, response_text])
|
| 46 |
|
| 47 |
return "", history
|
| 48 |
|
|
|
|
| 56 |
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
demo.launch()
|
| 60 |
+
# import gradio as gr
|
| 61 |
+
# import time
|
| 62 |
+
|
| 63 |
+
# def simple_chat_function(message, history):
|
| 64 |
+
# """Simplified chat function for testing"""
|
| 65 |
+
# if not message.strip():
|
| 66 |
+
# return "", history
|
| 67 |
+
|
| 68 |
+
# # Your response generation logic here
|
| 69 |
+
# response = f"You asked: {message}" # Replace with your actual logic
|
| 70 |
+
|
| 71 |
+
# # Add to history
|
| 72 |
+
# history.append([message, response])
|
| 73 |
+
|
| 74 |
+
# return "", history
|
| 75 |
+
|
| 76 |
+
# # Minimal working interface
|
| 77 |
+
# with gr.Blocks() as demo:
|
| 78 |
+
# chatbot = gr.Chatbot()
|
| 79 |
+
# msg = gr.Textbox(placeholder="Type your message here...")
|
| 80 |
+
# clear = gr.Button("Clear")
|
| 81 |
+
|
| 82 |
+
# msg.submit(simple_chat_function, [msg, chatbot], [msg, chatbot])
|
| 83 |
+
# clear.click(lambda: ([], ""), outputs=[chatbot, msg])
|
| 84 |
+
|
| 85 |
+
# if __name__ == "__main__":
|
| 86 |
+
# demo.launch()
|