Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,93 @@
|
|
| 1 |
-
|
| 2 |
from langchain_openai import ChatOpenAI
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
response = llm.invoke(message)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
Token Usage:
|
| 15 |
-
-----------
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
Total Tokens: {cb.total_tokens}
|
| 19 |
|
| 20 |
-
Cost Analysis:
|
| 21 |
-
-------------
|
| 22 |
Total Cost: ${cb.total_cost:.6f}
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from langchain_openai import ChatOpenAI
|
| 3 |
+
from langchain_community.callbacks import get_openai_callback
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def chat_with_tracking(message, api_key):
|
| 7 |
+
try:
|
| 8 |
+
# Set API key
|
| 9 |
+
os.environ["OPENAI_API_KEY"] = api_key
|
|
|
|
| 10 |
|
| 11 |
+
# Initialize ChatOpenAI with error handling
|
| 12 |
+
try:
|
| 13 |
+
llm = ChatOpenAI(
|
| 14 |
+
model="gpt-3.5-turbo", # Using 3.5-turbo as it's more reliable
|
| 15 |
+
temperature=0.7
|
| 16 |
+
)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
return f"Error initializing model: {str(e)}"
|
| 19 |
+
|
| 20 |
+
# Track token usage with error handling
|
| 21 |
+
try:
|
| 22 |
+
with get_openai_callback() as cb:
|
| 23 |
+
response = llm.invoke(message)
|
| 24 |
+
|
| 25 |
+
# Format the response with token usage
|
| 26 |
+
result = f"""
|
| 27 |
+
π€ Response:
|
| 28 |
+
{response.content}
|
| 29 |
|
| 30 |
+
π Token Usage Statistics:
|
| 31 |
+
-------------------------
|
| 32 |
+
π₯ Input Tokens: {cb.prompt_tokens}
|
| 33 |
+
π€ Output Tokens: {cb.completion_tokens}
|
| 34 |
+
π Total Tokens: {cb.total_tokens}
|
| 35 |
|
| 36 |
+
π° Cost Analysis:
|
| 37 |
+
---------------
|
| 38 |
Total Cost: ${cb.total_cost:.6f}
|
| 39 |
+
"""
|
| 40 |
+
return result
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error during token tracking: {str(e)}"
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"General error: {str(e)}"
|
| 47 |
+
finally:
|
| 48 |
+
# Clear API key from environment for security
|
| 49 |
+
if "OPENAI_API_KEY" in os.environ:
|
| 50 |
+
del os.environ["OPENAI_API_KEY"]
|
| 51 |
+
|
| 52 |
+
# Create Gradio interface with better styling
|
| 53 |
+
demo = gr.Interface(
|
| 54 |
+
fn=chat_with_tracking,
|
| 55 |
+
inputs=[
|
| 56 |
+
gr.Textbox(
|
| 57 |
+
label="Your Message",
|
| 58 |
+
placeholder="Type your message here...",
|
| 59 |
+
lines=4,
|
| 60 |
+
scale=2
|
| 61 |
+
),
|
| 62 |
+
gr.Textbox(
|
| 63 |
+
label="OpenAI API Key",
|
| 64 |
+
placeholder="Enter your OpenAI API key",
|
| 65 |
+
type="password",
|
| 66 |
+
scale=1
|
| 67 |
+
)
|
| 68 |
+
],
|
| 69 |
+
outputs=gr.Textbox(
|
| 70 |
+
label="Response and Analytics",
|
| 71 |
+
lines=10
|
| 72 |
+
),
|
| 73 |
+
title="π€ LangChain Token Usage Tracker",
|
| 74 |
+
description="Track token usage and costs for your conversations with GPT",
|
| 75 |
+
theme="default",
|
| 76 |
+
css="""
|
| 77 |
+
.gradio-container {
|
| 78 |
+
font-family: 'Arial', sans-serif;
|
| 79 |
+
}
|
| 80 |
+
.output-box {
|
| 81 |
+
font-family: 'Courier New', monospace;
|
| 82 |
+
}
|
| 83 |
+
"""
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Launch the application
|
| 87 |
+
if __name__ == "__main__":
|
| 88 |
+
demo.launch(
|
| 89 |
+
server_name="0.0.0.0",
|
| 90 |
+
server_port=7860,
|
| 91 |
+
share=False,
|
| 92 |
+
debug=True
|
| 93 |
+
)
|