pyresearch commited on
Commit
e443228
·
verified ·
1 Parent(s): 4cfe952

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -71
app.py CHANGED
@@ -2,31 +2,18 @@ import streamlit as st
2
  from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
3
  from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
4
  from clarifai_grpc.grpc.api.status import status_code_pb2
 
 
5
 
6
- # Set your Clarifai credentials and model details for NewsGuardian model
7
- PAT_GPT4 = '3ca5bd8b0f2244eb8d0e4b2838fc3cf1'
8
- USER_ID_GPT4 = 'openai'
9
- APP_ID_GPT4 = 'chat-completion'
10
- MODEL_ID_GPT4 = 'openai-gpt-4-vision'
11
- MODEL_VERSION_ID_GPT4 = '266df29bc09843e0aee9b7bf723c03c2'
12
 
13
- # Set your Clarifai credentials and model details for DALL-E
14
- PAT_DALLE = 'bfdeb4029ef54d23a2e608b0aa4c00e4'
15
- USER_ID_DALLE = 'openai'
16
- APP_ID_DALLE = 'dall-e'
17
- MODEL_ID_DALLE = 'dall-e-3'
18
- MODEL_VERSION_ID_DALLE = 'dc9dcb6ee67543cebc0b9a025861b868'
19
 
20
 
 
21
 
22
- # Streamlit app
23
- # Set your Clarifai credentials for NewsGuardian model
24
- PAT_TTS = 'bfdeb4029ef54d23a2e608b0aa4c00e4'
25
- USER_ID_TTS = 'openai'
26
- APP_ID_TTS = 'tts'
27
- MODEL_ID_TTS = 'openai-tts-1'
28
- MODEL_VERSION_ID_TTS = 'fff6ce1fd487457da95b79241ac6f02d'
29
-
30
 
31
  # Set up gRPC channel for NewsGuardian model
32
  channel_tts = ClarifaiChannel.get_grpc_channel()
@@ -37,18 +24,16 @@ userDataObject_tts = resources_pb2.UserAppIDSet(user_id=USER_ID_TTS, app_id=APP_
37
  # Streamlit app
38
  st.title("NewsGuardian")
39
 
40
-
41
  # Inserting logo
42
  st.image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTdA-MJ_SUCRgLs1prqudpMdaX4x-x10Zqlwp7cpzXWCMM9xjBAJYWdJsDlLoHBqNpj8qs&usqp=CAU")
 
43
  # Function to get gRPC channel for NewsGuardian model
44
  def get_tts_channel():
45
  channel_tts = ClarifaiChannel.get_grpc_channel()
46
  return channel_tts, channel_tts.metadata
47
 
48
-
49
-
50
  # User input
51
- model_type = st.selectbox("Select Model", ["NewsGuardian model","NewsGuardian model"])
52
  raw_text = st.text_area("This news is real or fake?")
53
  image_upload = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
54
 
@@ -96,8 +81,6 @@ if st.button("NewsGuardian News Result"):
96
  st.text(output_gpt4.text.raw)
97
 
98
  # Convert text to speech and play the audio
99
- stub_tts = service_pb2_grpc.V2Stub(channel_gpt4) # Use the same channel for TTS
100
-
101
  tts_input_data = resources_pb2.Data()
102
  tts_input_data.text.raw = output_gpt4.text.raw
103
 
@@ -108,7 +91,7 @@ if st.button("NewsGuardian News Result"):
108
  version_id=MODEL_VERSION_ID_TTS,
109
  inputs=[resources_pb2.Input(data=tts_input_data)]
110
  ),
111
- metadata=metadata_gpt4 # Use the same metadata for TTS
112
  )
113
 
114
  # Check if the TTS request was successful
@@ -154,44 +137,6 @@ if st.button("NewsGuardian News Result"):
154
  elif output_dalle.HasField("text"):
155
  st.text(output_dalle.text.raw)
156
 
157
- elif model_type == "NewsGuardian model":
158
- # Set up gRPC channel for NewsGuardian model
159
- channel_tts = ClarifaiChannel.get_grpc_channel()
160
- stub_tts = service_pb2_grpc.V2Stub(channel_tts)
161
- metadata_tts = (('authorization', 'Key ' + PAT_TTS),)
162
- userDataObject_tts = resources_pb2.UserAppIDSet(user_id=USER_ID_TTS, app_id=APP_ID_TTS)
163
-
164
- # Prepare the request for NewsGuardian model
165
- input_data_tts = resources_pb2.Data()
166
-
167
- if raw_text:
168
- input_data_tts.text.raw = raw_text
169
-
170
- post_model_outputs_response_tts = stub_tts.PostModelOutputs(
171
- service_pb2.PostModelOutputsRequest(
172
- user_app_id=userDataObject_tts,
173
- model_id=MODEL_ID_TTS,
174
- version_id=MODEL_VERSION_ID_TTS,
175
- inputs=[resources_pb2.Input(data=input_data_tts)]
176
- ),
177
- metadata=metadata_tts
178
- )
179
-
180
- # Check if the request was successful for NewsGuardian model
181
- if post_model_outputs_response_tts.status.code != status_code_pb2.SUCCESS:
182
- st.error(f"NewsGuardian model API request failed: {post_model_outputs_response_tts.status.description}")
183
- else:
184
- # Get the output for NewsGuardian model
185
- output_tts = post_model_outputs_response_tts.outputs[0].data
186
-
187
- # Display the result for NewsGuardian model
188
- if output_tts.HasField("text"):
189
- st.text(output_tts.text.raw)
190
-
191
- if output_tts.HasField("audio"):
192
- st.audio(output_tts.audio.base64, format='audio/wav')
193
-
194
-
195
  # Add the beautiful social media icon section
196
  st.markdown("""
197
  <div align="center">
@@ -215,9 +160,3 @@ st.markdown("""
215
  </div>
216
  <hr>
217
  """, unsafe_allow_html=True)
218
-
219
-
220
-
221
-
222
-
223
-
 
2
  from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
3
  from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
4
  from clarifai_grpc.grpc.api.status import status_code_pb2
5
+ from transformers import AutoModelForCausalLM, AutoTokenizer
6
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
7
 
8
+ import torch
 
 
 
 
 
9
 
 
 
 
 
 
 
10
 
11
 
12
+ torch.set_default_device("cpu")
13
 
14
+ # Load the 'microsoft/phi-2' model and tokenizer
15
+ model_phi2 = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype="auto", trust_remote_code=True)
16
+ tokenizer_phi2 = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)
 
 
 
 
 
17
 
18
  # Set up gRPC channel for NewsGuardian model
19
  channel_tts = ClarifaiChannel.get_grpc_channel()
 
24
  # Streamlit app
25
  st.title("NewsGuardian")
26
 
 
27
  # Inserting logo
28
  st.image("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTdA-MJ_SUCRgLs1prqudpMdaX4x-x10Zqlwp7cpzXWCMM9xjBAJYWdJsDlLoHBqNpj8qs&usqp=CAU")
29
+
30
  # Function to get gRPC channel for NewsGuardian model
31
  def get_tts_channel():
32
  channel_tts = ClarifaiChannel.get_grpc_channel()
33
  return channel_tts, channel_tts.metadata
34
 
 
 
35
  # User input
36
+ model_type = st.selectbox("Select Model", ["NewsGuardian model", "DALL-E"])
37
  raw_text = st.text_area("This news is real or fake?")
38
  image_upload = st.file_uploader("Upload Image", type=["jpg", "jpeg", "png"])
39
 
 
81
  st.text(output_gpt4.text.raw)
82
 
83
  # Convert text to speech and play the audio
 
 
84
  tts_input_data = resources_pb2.Data()
85
  tts_input_data.text.raw = output_gpt4.text.raw
86
 
 
91
  version_id=MODEL_VERSION_ID_TTS,
92
  inputs=[resources_pb2.Input(data=tts_input_data)]
93
  ),
94
+ metadata=metadata_tts # Use the same metadata for TTS
95
  )
96
 
97
  # Check if the TTS request was successful
 
137
  elif output_dalle.HasField("text"):
138
  st.text(output_dalle.text.raw)
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  # Add the beautiful social media icon section
141
  st.markdown("""
142
  <div align="center">
 
160
  </div>
161
  <hr>
162
  """, unsafe_allow_html=True)