Zekun Wu commited on
Commit
0b5c5aa
1 Parent(s): 16a4a07
Files changed (2) hide show
  1. app.py +15 -9
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,6 +1,8 @@
 
 
1
  import json
2
  import http.client
3
- import streamlit as st
4
  from openai import AzureOpenAI
5
 
6
  class ContentFormatter:
@@ -14,18 +16,20 @@ class ContentFormatter:
14
  return json.dumps(data)
15
 
16
  class AzureAgent:
17
- def __init__(self, api_key, azure_uri):
18
  self.azure_uri = azure_uri
19
  self.headers = {
20
  'Authorization': f"Bearer {api_key}",
21
  'Content-Type': 'application/json'
22
  }
 
 
23
  self.chat_formatter = ContentFormatter
24
 
25
  def invoke(self, text, **kwargs):
26
  body = self.chat_formatter.chat_completions(text, {**kwargs})
27
  conn = http.client.HTTPSConnection(self.azure_uri)
28
- conn.request("POST", '/v1/chat/completions', body=body, headers=self.headers)
29
  response = conn.getresponse()
30
  data = response.read()
31
  conn.close()
@@ -35,13 +39,13 @@ class AzureAgent:
35
  return content
36
 
37
  class GPTAgent:
38
- def __init__(self, api_key, azure_endpoint):
39
  self.client = AzureOpenAI(
40
  api_key=api_key,
41
- api_version='2022-11-01', # Defaulting to a specific API version
42
  azure_endpoint=azure_endpoint
43
  )
44
- self.deployment_name = 'your-model-deployment-name' # Update this based on actual deployment name
45
 
46
  def invoke(self, text, **kwargs):
47
  response = self.client.chat.completions.create(
@@ -60,6 +64,8 @@ st.title('Model Invocation with Dataset Processing')
60
  model_type = st.radio("Select the type of agent", ('AzureAgent', 'GPTAgent'))
61
  api_key = st.text_input("API Key", type="password")
62
  endpoint_url = st.text_input("Endpoint URL")
 
 
63
 
64
  # File upload and data display
65
  uploaded_file = st.file_uploader("Choose a file")
@@ -72,9 +78,9 @@ if uploaded_file is not None:
72
  # Process data button
73
  if st.button('Process Data'):
74
  if model_type == 'AzureAgent':
75
- agent = AzureAgent(api_key, endpoint_url)
76
  else:
77
- agent = GPTAgent(api_key, endpoint_url)
78
 
79
  # Example processing step (adapt as needed)
80
  df['Response'] = df['Input Column'].apply(lambda x: agent.invoke(x, temperature=0, max_tokens=150))
@@ -87,4 +93,4 @@ if uploaded_file is not None:
87
  data=df.to_csv().encode('utf-8'),
88
  file_name='processed_data.csv',
89
  mime='text/csv',
90
- )
 
1
+ import streamlit as st
2
+ import pandas as pd
3
  import json
4
  import http.client
5
+ from io import StringIO
6
  from openai import AzureOpenAI
7
 
8
  class ContentFormatter:
 
16
  return json.dumps(data)
17
 
18
  class AzureAgent:
19
+ def __init__(self, api_key, azure_uri, deployment_name, api_version):
20
  self.azure_uri = azure_uri
21
  self.headers = {
22
  'Authorization': f"Bearer {api_key}",
23
  'Content-Type': 'application/json'
24
  }
25
+ self.deployment_name = deployment_name
26
+ self.api_version = api_version
27
  self.chat_formatter = ContentFormatter
28
 
29
  def invoke(self, text, **kwargs):
30
  body = self.chat_formatter.chat_completions(text, {**kwargs})
31
  conn = http.client.HTTPSConnection(self.azure_uri)
32
+ conn.request("POST", f'/v{self.api_version}/chat/completions', body=body, headers=self.headers)
33
  response = conn.getresponse()
34
  data = response.read()
35
  conn.close()
 
39
  return content
40
 
41
  class GPTAgent:
42
+ def __init__(self, api_key, azure_endpoint, deployment_name, api_version='2022-11-01'):
43
  self.client = AzureOpenAI(
44
  api_key=api_key,
45
+ api_version=api_version,
46
  azure_endpoint=azure_endpoint
47
  )
48
+ self.deployment_name = deployment_name
49
 
50
  def invoke(self, text, **kwargs):
51
  response = self.client.chat.completions.create(
 
64
  model_type = st.radio("Select the type of agent", ('AzureAgent', 'GPTAgent'))
65
  api_key = st.text_input("API Key", type="password")
66
  endpoint_url = st.text_input("Endpoint URL")
67
+ deployment_name = st.text_input("Model Name")
68
+ api_version = st.text_input("API Version") # Default API version
69
 
70
  # File upload and data display
71
  uploaded_file = st.file_uploader("Choose a file")
 
78
  # Process data button
79
  if st.button('Process Data'):
80
  if model_type == 'AzureAgent':
81
+ agent = AzureAgent(api_key, endpoint_url, deployment_name, api_version)
82
  else:
83
+ agent = GPTAgent(api_key, endpoint_url, deployment_name, api_version)
84
 
85
  # Example processing step (adapt as needed)
86
  df['Response'] = df['Input Column'].apply(lambda x: agent.invoke(x, temperature=0, max_tokens=150))
 
93
  data=df.to_csv().encode('utf-8'),
94
  file_name='processed_data.csv',
95
  mime='text/csv',
96
+ )
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- openai
 
 
1
+ openai
2
+ pandas