DexterSptizu commited on
Commit
4abc14c
Β·
verified Β·
1 Parent(s): f0dde89

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -18
app.py CHANGED
@@ -1,24 +1,93 @@
1
- from langchain_community.callbacks import get_openai_callback
2
  from langchain_openai import ChatOpenAI
 
 
3
 
4
- def track_tokens_and_cost(message):
5
- llm = ChatOpenAI(temperature=0.7)
6
-
7
- with get_openai_callback() as cb:
8
- response = llm.invoke(message)
9
 
10
- # Access the correct attributes
11
- print(f"""
12
- Response: {response.content}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- Token Usage:
15
- -----------
16
- Prompt Tokens: {cb.prompt_tokens}
17
- Completion Tokens: {cb.completion_tokens}
18
- Total Tokens: {cb.total_tokens}
19
 
20
- Cost Analysis:
21
- -------------
22
  Total Cost: ${cb.total_cost:.6f}
23
- """)
24
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ )